fork download
  1. #module
  2. #deffunc hoge array map, array distance, int w, local min, local count
  3. repeat -1, 1
  4. min=cnt
  5. count=0
  6. foreach map
  7. if distance(cnt)!min : continue
  8. if map(cnt-1)=0 and distance(cnt-1)=0 : distance(cnt-1)=min+1
  9. if map(cnt+1)=0 and distance(cnt+1)=0 : distance(cnt+1)=min+1
  10. if map(cnt-w)=0 and distance(cnt-w)=0 : distance(cnt-w)=min+1
  11. if map(cnt+w)=0 and distance(cnt+w)=0 : distance(cnt+w)=min+1
  12. count++
  13. loop
  14. if count=0 : break
  15. loop
  16. return
  17.  
  18. #defcfunc fuga array distance, int w, int sx, int sy, local result, local i, local p
  19. result=""
  20. p=sy*w+sx
  21. for i, distance(p), 1, -1
  22. switch i-1
  23. case distance(p-1) : result+="l" : p=p-1 : swbreak
  24. case distance(p+1) : result+="r" : p=p+1 : swbreak
  25. case distance(p-w) : result+="u" : p=p-w : swbreak
  26. case distance(p+w) : result+="d" : p=p+w : swbreak
  27. swend
  28. next
  29. return result
  30.  
  31. #defcfunc solve array map_str, local map, local w, local h, local sx, local sy, local gx, local gy, local distance
  32. w=strlen(map_str.0)
  33. h=length(map_str)
  34. dim map, w*h
  35. dim distance, w*h
  36. for y, 0, h
  37. for x, 0, w
  38. switch peek(map_str.y, x)
  39. case 'S' : sx=x : sy=y : swbreak
  40. case 'G' : gx=x : gy=y : swbreak
  41. case '#' : map(y*w+x)=1 : swbreak
  42. swend
  43. next
  44. next
  45. distance(gy*w+gx)=1
  46.  
  47. hoge map, distance, w
  48.  
  49. if distance(sy*w+sx)=0 : return "No route"
  50.  
  51. return fuga(distance, w, sx, sy)
  52. #global
  53.  
  54.  
  55. map1="#######", "#..S..#", "#.....#", "#..G..#", "#######"
  56. mes solve(map1)
  57.  
  58. map2="#######", "#.....#", "#.G.#.#", "#..#..#", "#.#.S.#", "#.....#", "#######"
  59. mes solve(map2)
  60.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty