Index: test.d ================================================================== --- test.d +++ test.d @@ -187,66 +187,75 @@ sy=y, sx=x; else if(data[y][x]=='\\') ly~=y, lx~=x; else if(data[y][x]=='O') oy=y, ox=x; - if(ly.length==0) - return goal(sy,sx,oy,ox); + if(ly.length==0) { + auto r = search(sy,sx,oy,ox); + switch(r[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(); } - int goal(int sy, int sx, int oy, int ox) + Tuple!(char,int) search(int sy, int sx, int oy, int ox) { alias Tuple!(int,"y",int,"x") Pt; Pt[] q = [Pt(oy,ox)]; - while(q.length) { + 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(y==sy && x==sx) { - if(i==0) return command_D(); - if(i==1) return command_U(); - if(i==2) return command_R(); - if(i==3) return command_L(); - } else if(data[y][x]==' ') { + 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); } else if(data[y][x]=='.' && data[y-1][x]!='*') { q2 ~= Pt(y,x); } } } q = q2; } q = [Pt(oy,ox)]; - while(q.length) { + 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(y==sy && x==sx) { - if(i==0) return command_D(); - if(i==1) return command_U(); - if(i==2) return command_R(); - if(i==3) return command_L(); - } else if(data[y][x]==' ') { + 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); } else if(data[y][x]=='.'/* && data[y-1][x]!='*'*/) { q2 ~= Pt(y,x); } } } q = q2; } - return abort(); + return tuple('A',int.max); } } class MyForm : Form {