#module
#deffunc hoge array map, array distance, int w, local min, local count
repeat -1, 1
min=cnt
count=0
foreach map
if distance(cnt)!min : continue
if map(cnt-1)=0 and distance(cnt-1)=0 : distance(cnt-1)=min+1
if map(cnt+1)=0 and distance(cnt+1)=0 : distance(cnt+1)=min+1
if map(cnt-w)=0 and distance(cnt-w)=0 : distance(cnt-w)=min+1
if map(cnt+w)=0 and distance(cnt+w)=0 : distance(cnt+w)=min+1
count++
loop
if count=0 : break
loop
return
#defcfunc fuga array distance, int w, int sx, int sy, local result, local i, local p
result=""
p=sy*w+sx
for i, distance(p), 1, -1
switch i-1
case distance(p-1) : result+="l" : p=p-1 : swbreak
case distance(p+1) : result+="r" : p=p+1 : swbreak
case distance(p-w) : result+="u" : p=p-w : swbreak
case distance(p+w) : result+="d" : p=p+w : swbreak
swend
next
return result
#defcfunc solve array map_str, local map, local w, local h, local sx, local sy, local gx, local gy, local distance
w=strlen(map_str.0)
h=length(map_str)
dim map, w*h
dim distance, w*h
for y, 0, h
for x, 0, w
switch peek(map_str.y, x)
case 'S' : sx=x : sy=y : swbreak
case 'G' : gx=x : gy=y : swbreak
case '#' : map(y*w+x)=1 : swbreak
swend
next
next
distance(gy*w+gx)=1
hoge map, distance, w
if distance(sy*w+sx)=0 : return "No route"
return fuga(distance, w, sx, sy)
#global
map1="#######", "#..S..#", "#.....#", "#..G..#", "#######"
mes solve(map1)
map2="#######", "#.....#", "#.G.#.#", "#..#..#", "#.#.S.#", "#.....#", "#######"
mes solve(map2)
I21vZHVsZQojZGVmZnVuYyBob2dlIGFycmF5IG1hcCwgYXJyYXkgZGlzdGFuY2UsIGludCB3LCBsb2NhbCBtaW4sIGxvY2FsIGNvdW50CglyZXBlYXQgLTEsIDEKCQltaW49Y250CgkJY291bnQ9MAoJCWZvcmVhY2ggbWFwCgkJCWlmIGRpc3RhbmNlKGNudCkhbWluIDogY29udGludWUKCQkJaWYgbWFwKGNudC0xKT0wIGFuZCBkaXN0YW5jZShjbnQtMSk9MCA6IGR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