rt,代码:
#include<iostream>
#include<queue>
using namespace std;
struct dat{
int x,y,dir,tm;
};
int n,m;
bool mp[55][55],vis[55][55][4];
dat from[55][55][4];
int sx,sy,ex,ey;
char sdir;
int dec_dir[128];
char chr_dir[4]={'N','E','S','W'};
int mv[5]={1,2,3,0,0};
int rot[5]={0,0,0,3,1};
int fx[4]={-1,0,1,0};
int fy[4]={0,1,0,-1};
int bfs(){
dec_dir['N']=0;
dec_dir['E']=1;
dec_dir['S']=2;
dec_dir['W']=3;
queue<dat> q;
q.push({sx,sy,dec_dir[sdir],0});
vis[sx][sy][dec_dir[sdir]]=true;
while(!q.empty()){
dat cur=q.front();
q.pop();
int x=cur.x,y=cur.y,dir=cur.dir;
// cout<<"pos:("<<x<<","<<y<<") dir:"<<chr_dir[dir]<<" stp:"<<cur.tm<<" ->"<<endl;
if(x==ex&&y==ey){
// cout<<"+---To destination.Route backtrace:"<<endl;
dat bkt=cur;
while(bkt.x!=sx||bkt.y!=sy||bkt.dir!=dec_dir[sdir]){
// cout<<"| +--- pos:("<<bkt.x<<","<<bkt.y<<") dir:"<<chr_dir[bkt.dir]<<" stp:"<<bkt.tm<<endl;
bkt=from[bkt.x][bkt.y][bkt.dir];
}
return cur.tm;
}
for(int i=0;i<5;i++){
int x2=x+fx[dir]*mv[i],y2=y+fy[dir]*mv[i],dir2=(dir+rot[i])%4;
// if(x2<=0||x2>=n||y2<=0||y2>=n)cout<<"+---[OP "<<i+1<<"] : Out of range"<<endl;
if(x2<=0||x2>=n||y2<=0||y2>=n)continue;
// if(vis[x2][y2][dir2])cout<<"+---[OP "<<i+1<<"] : Visited"<<endl;
if(vis[x2][y2][dir2])continue;
bool flg=false;
for(int j=1;j<=mv[i];j++){
int x3=x+fx[dir]*j,y3=y+fy[dir]*j;
if(mp[x3][y3]){
// cout<<"+---[OP "<<i+1<<"] : Wall at ("<<x3<<","<<y3<<")"<<endl;
flg=true;
break;
}
}
if(flg)continue;
// cout<<"+---[OP "<<i+1<<"] : To pos:("<<x2<<","<<y2<<") dir:"<<chr_dir[dir2]<<endl;
from[x2][y2][dir2]=cur;
q.push({x2,y2,dir2,cur.tm+1});
vis[x2][y2][dir2]=true;
}
}
return -1;
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int cur;
cin>>cur;
if(cur){
mp[i][j]=mp[i+1][j]=mp[i][j+1]=mp[i+1][j+1]=true;
}
}
}
cin>>sx>>sy>>ex>>ey>>sdir;
cout<<bfs();
}
其中#3的输入如下:
6 7
0 0 0 0 0 0 0
0 0 0 0 1 0 0
0 0 1 0 0 0 0
0 0 1 0 0 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 0
1 1 1 6 E
答案11输出-1。调试输出如下:
pos:(1,1) dir:E stp:0 ->
+---[OP 1] : To pos:(1,2) dir:E
+---[OP 2] : To pos:(1,3) dir:E
+---[OP 3] : Wall at (1,4)
+---[OP 4] : To pos:(1,1) dir:N
+---[OP 5] : To pos:(1,1) dir:S
pos:(1,2) dir:E stp:1 ->
+---[OP 1] : Visited
+---[OP 2] : Wall at (1,4)
+---[OP 3] : Wall at (1,4)
+---[OP 4] : To pos:(1,2) dir:N
+---[OP 5] : To pos:(1,2) dir:S
pos:(1,3) dir:E stp:1 ->
+---[OP 1] : Wall at (1,4)
+---[OP 2] : Wall at (1,4)
+---[OP 3] : Out of range
+---[OP 4] : To pos:(1,3) dir:N
+---[OP 5] : To pos:(1,3) dir:S
pos:(1,1) dir:N stp:1 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : To pos:(1,1) dir:W
+---[OP 5] : Visited
pos:(1,1) dir:S stp:1 ->
+---[OP 1] : To pos:(2,1) dir:S
+---[OP 2] : To pos:(3,1) dir:S
+---[OP 3] : To pos:(4,1) dir:S
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(1,2) dir:N stp:2 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : To pos:(1,2) dir:W
+---[OP 5] : Visited
pos:(1,2) dir:S stp:2 ->
+---[OP 1] : Wall at (2,2)
+---[OP 2] : Wall at (2,2)
+---[OP 3] : Wall at (2,2)
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(1,3) dir:N stp:2 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : To pos:(1,3) dir:W
+---[OP 5] : Visited
pos:(1,3) dir:S stp:2 ->
+---[OP 1] : Wall at (2,3)
+---[OP 2] : Wall at (2,3)
+---[OP 3] : Wall at (2,3)
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(1,1) dir:W stp:2 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(2,1) dir:S stp:2 ->
+---[OP 1] : Visited
+---[OP 2] : Visited
+---[OP 3] : To pos:(5,1) dir:S
+---[OP 4] : To pos:(2,1) dir:E
+---[OP 5] : To pos:(2,1) dir:W
pos:(3,1) dir:S stp:2 ->
+---[OP 1] : Visited
+---[OP 2] : Visited
+---[OP 3] : Out of range
+---[OP 4] : To pos:(3,1) dir:E
+---[OP 5] : To pos:(3,1) dir:W
pos:(4,1) dir:S stp:2 ->
+---[OP 1] : Visited
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : To pos:(4,1) dir:E
+---[OP 5] : To pos:(4,1) dir:W
pos:(1,2) dir:W stp:3 ->
+---[OP 1] : Visited
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(1,3) dir:W stp:3 ->
+---[OP 1] : Visited
+---[OP 2] : Visited
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(5,1) dir:S stp:3 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : To pos:(5,1) dir:E
+---[OP 5] : To pos:(5,1) dir:W
pos:(2,1) dir:E stp:3 ->
+---[OP 1] : Wall at (2,2)
+---[OP 2] : Wall at (2,2)
+---[OP 3] : Wall at (2,2)
+---[OP 4] : To pos:(2,1) dir:N
+---[OP 5] : Visited
pos:(2,1) dir:W stp:3 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(3,1) dir:E stp:3 ->
+---[OP 1] : Wall at (3,2)
+---[OP 2] : Wall at (3,2)
+---[OP 3] : Wall at (3,2)
+---[OP 4] : To pos:(3,1) dir:N
+---[OP 5] : Visited
pos:(3,1) dir:W stp:3 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(4,1) dir:E stp:3 ->
+---[OP 1] : Wall at (4,2)
+---[OP 2] : Wall at (4,2)
+---[OP 3] : Wall at (4,2)
+---[OP 4] : To pos:(4,1) dir:N
+---[OP 5] : Visited
pos:(4,1) dir:W stp:3 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(5,1) dir:E stp:4 ->
+---[OP 1] : To pos:(5,2) dir:E
+---[OP 2] : To pos:(5,3) dir:E
+---[OP 3] : To pos:(5,4) dir:E
+---[OP 4] : To pos:(5,1) dir:N
+---[OP 5] : Visited
pos:(5,1) dir:W stp:4 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(2,1) dir:N stp:4 ->
+---[OP 1] : Visited
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(3,1) dir:N stp:4 ->
+---[OP 1] : Visited
+---[OP 2] : Visited
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(4,1) dir:N stp:4 ->
+---[OP 1] : Visited
+---[OP 2] : Visited
+---[OP 3] : Visited
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(5,2) dir:E stp:5 ->
+---[OP 1] : Visited
+---[OP 2] : Visited
+---[OP 3] : Wall at (5,5)
+---[OP 4] : To pos:(5,2) dir:N
+---[OP 5] : To pos:(5,2) dir:S
pos:(5,3) dir:E stp:5 ->
+---[OP 1] : Visited
+---[OP 2] : Wall at (5,5)
+---[OP 3] : Out of range
+---[OP 4] : To pos:(5,3) dir:N
+---[OP 5] : To pos:(5,3) dir:S
pos:(5,4) dir:E stp:5 ->
+---[OP 1] : Wall at (5,5)
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : To pos:(5,4) dir:N
+---[OP 5] : To pos:(5,4) dir:S
pos:(5,1) dir:N stp:5 ->
+---[OP 1] : Visited
+---[OP 2] : Visited
+---[OP 3] : Visited
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(5,2) dir:N stp:6 ->
+---[OP 1] : Wall at (4,2)
+---[OP 2] : Wall at (4,2)
+---[OP 3] : Wall at (4,2)
+---[OP 4] : To pos:(5,2) dir:W
+---[OP 5] : Visited
pos:(5,2) dir:S stp:6 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(5,3) dir:N stp:6 ->
+---[OP 1] : Wall at (4,3)
+---[OP 2] : Wall at (4,3)
+---[OP 3] : Wall at (4,3)
+---[OP 4] : To pos:(5,3) dir:W
+---[OP 5] : Visited
pos:(5,3) dir:S stp:6 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(5,4) dir:N stp:6 ->
+---[OP 1] : To pos:(4,4) dir:N
+---[OP 2] : To pos:(3,4) dir:N
+---[OP 3] : Wall at (2,4)
+---[OP 4] : To pos:(5,4) dir:W
+---[OP 5] : Visited
pos:(5,4) dir:S stp:6 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(5,2) dir:W stp:7 ->
+---[OP 1] : Visited
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(5,3) dir:W stp:7 ->
+---[OP 1] : Visited
+---[OP 2] : Visited
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(4,4) dir:N stp:7 ->
+---[OP 1] : Visited
+---[OP 2] : Wall at (2,4)
+---[OP 3] : Wall at (2,4)
+---[OP 4] : To pos:(4,4) dir:W
+---[OP 5] : To pos:(4,4) dir:E
pos:(3,4) dir:N stp:7 ->
+---[OP 1] : Wall at (2,4)
+---[OP 2] : Wall at (2,4)
+---[OP 3] : Out of range
+---[OP 4] : To pos:(3,4) dir:W
+---[OP 5] : To pos:(3,4) dir:E
pos:(5,4) dir:W stp:7 ->
+---[OP 1] : Visited
+---[OP 2] : Visited
+---[OP 3] : Visited
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(4,4) dir:W stp:8 ->
+---[OP 1] : Wall at (4,3)
+---[OP 2] : Wall at (4,3)
+---[OP 3] : Visited
+---[OP 4] : To pos:(4,4) dir:S
+---[OP 5] : Visited
pos:(4,4) dir:E stp:8 ->
+---[OP 1] : Wall at (4,5)
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(3,4) dir:W stp:8 ->
+---[OP 1] : Wall at (3,3)
+---[OP 2] : Wall at (3,3)
+---[OP 3] : Visited
+---[OP 4] : To pos:(3,4) dir:S
+---[OP 5] : Visited
pos:(3,4) dir:E stp:8 ->
+---[OP 1] : To pos:(3,5) dir:E
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(4,4) dir:S stp:9 ->
+---[OP 1] : Visited
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(3,4) dir:S stp:9 ->
+---[OP 1] : Visited
+---[OP 2] : Visited
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(3,5) dir:E stp:9 ->
+---[OP 1] : Out of range
+---[OP 2] : Out of range
+---[OP 3] : Out of range
+---[OP 4] : To pos:(3,5) dir:N
+---[OP 5] : To pos:(3,5) dir:S
pos:(3,5) dir:N stp:10 ->
+---[OP 1] : Wall at (2,5)
+---[OP 2] : Wall at (2,5)
+---[OP 3] : Out of range
+---[OP 4] : To pos:(3,5) dir:W
+---[OP 5] : Visited
pos:(3,5) dir:S stp:10 ->
+---[OP 1] : Wall at (4,5)
+---[OP 2] : Wall at (4,5)
+---[OP 3] : Out of range
+---[OP 4] : Visited
+---[OP 5] : Visited
pos:(3,5) dir:W stp:11 ->
+---[OP 1] : Visited
+---[OP 2] : Wall at (3,3)
+---[OP 3] : Wall at (3,3)
+---[OP 4] : Visited
+---[OP 5] : Visited
-1