Index: test.d ================================================================== --- test.d +++ test.d @@ -197,59 +197,85 @@ case 'L': return command_L(); case 'R': return command_R(); case 'A': return abort(); default: return wait(); } + } else { + Tuple!(char,int)[] cand; + for(int i=0; i<ly.length; ++i) { + auto r = search(sy,sx,ly[i],lx[i]); + cand ~= r; + } + sort!((Tuple!(char,int) c1, Tuple!(char,int) c2){ + if(c1[1] != c2[1]) + return c1[1] < c2[1]; + return c1[0] < c2[0]; + })(cand); + switch(cand[0][0]) { + case 'D': return command_D(); + case 'U': return command_U(); + case 'L': return command_L(); + case 'R': return command_R(); + case 'A': return abort(); + default: return wait(); + } } return wait(); } Tuple!(char,int) search(int sy, int sx, int oy, int ox) { alias Tuple!(int,"y",int,"x") Pt; Pt[] q = [Pt(oy,ox)]; + bool[][] v = new bool[][](H,W); for(int step=1; q.length; ++step) { Pt[] q2; foreach(p; q) { - int[] dy=[-1,+1,0,0]; int[] dx=[0,0,-1,+1]; for(int i=0; i<4; ++i) { int y = p.y+dy[i]; int x = p.x+dx[i]; + if(v[y][x]) continue; if(y==sy && x==sx) { if(i==0) return tuple('D',step); if(i==1) return tuple('U',step); if(i==2) return tuple('R',step); if(i==3) return tuple('L',step); } else if(data[y][x]==' '||data[y][x]=='\\') { q2 ~= Pt(y,x); + v[y][x]=true; } else if(data[y][x]=='.' && data[y-1][x]!='*') { q2 ~= Pt(y,x); + v[y][x]=true; } } } q = q2; } q = [Pt(oy,ox)]; + v = new bool[][](H,W); for(int step=1<<10; q.length; ++step) { Pt[] q2; foreach(p; q) { int[] dy=[-1,+1,0,0]; int[] dx=[0,0,-1,+1]; for(int i=0; i<4; ++i) { int y = p.y+dy[i]; int x = p.x+dx[i]; + if(v[y][x]) continue; if(y==sy && x==sx) { if(i==0) return tuple('D',step); if(i==1) return tuple('U',step); if(i==2) return tuple('R',step); if(i==3) return tuple('L',step); } else if(data[y][x]==' '||data[y][x]=='\\') { q2 ~= Pt(y,x); + v[y][x]=true; } else if(data[y][x]=='.'/* && data[y-1][x]!='*'*/) { q2 ~= Pt(y,x); + v[y][x]=true; } } } q = q2; }