fork download
  1. def f(deep,map1,s1,x,y,h,w)
  2. return 0 if map1[y][x]!=s1[deep]
  3. if deep==7 then
  4. return 1
  5. else
  6. res=0
  7.  
  8. [[1,0],[0,1],[-1,0],[0,-1]].each{|dx,dy|
  9. x2=x+dx
  10. y2=y+dy
  11. next if x2<0 || w<=x2 || y2<0 || h<=y2
  12. t=map1[y][x]
  13. map1[y][x]="" if t!="A" && t!="O"
  14. res+=f(deep+1,map1,s1,x2,y2,h,w)
  15. map1[y][x]=t
  16. }
  17. return res
  18. end
  19. end
  20. h,w=gets.split(" ").map{|e| e.to_i}
  21. s1=["Y","O","K","O","H","A","M","A"]
  22. map1=[]
  23. h.times{
  24. map1<<gets.chomp.split("")
  25. }
  26. ans=0
  27. h.times{|y|
  28. w.times{|x|
  29. t=f(0,map1,s1,x,y,h,w)
  30. ans+=t
  31. }
  32. }
  33. puts ans
Success #stdin #stdout 0.02s 8176KB
stdin
3 6
MAYOHA
AHOKAM
MAYOHA
stdout
80