rt. 一直不知道哪里有问题。代码应该很易懂吧……
#include<bits/stdc++.h>
#define ll long long
using namespace std;
template<class T>inline void rd(T &s){
int f=1; s=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f*=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
s*=f;
}
//北、南、西、东
const int dx[]={114514,-1,1,0,0};
const int dy[]={114514,0,0,-1,1};
int mp[55][55],n,m;
int sx,sy,ex,ey;
bool vis[55][55];
int ans[55][55];
char ch;
bool check(int x,int y){
if(x<1||x>=n||y<1||y>=m) return 0;
if(mp[x][y]||mp[x][y+1]||mp[x+1][y]||mp[x+1][y+1]) return 0;
return 1;
}
struct node{
int x,y,drt;//drt为方向
//north=1,south=2,west=3,east=4;
};
queue<node> Q;
void bfs(){
while(!Q.empty()){
node now=Q.front();
Q.pop();
for(int i=1;i<=4;i++){
for(int j=1;j<=3;j++){
int tx=now.x+dx[i]*j,ty=now.y+dy[i]*j;
if(!check(tx,ty)||vis[tx][ty]) break;
vis[tx][ty]=1;
//printf("tx=%d ty=%d drt=%d\n",tx,ty,i);
ans[tx][ty]=ans[now.x][now.y]+1;
if(now.drt!=i) ans[tx][ty]++;//换方向了,时间+1
if(tx==ex&&ty==ey) return;
Q.push((node){tx,ty,i});
}
}
}
}
int main(){
rd(n),rd(m);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
rd(mp[i][j]);
}
}
rd(sx),rd(sy),rd(ex),rd(ey);
cin>>ch;
int d=0;
switch(ch){
case 'N': d=1; break;
case 'S': d=2; break;
case 'W': d=3; break;
default : d=4;
}
Q.push((node){sx,sy,d});
vis[sx][sy]=1;
bfs();
if(vis[ex][ey]==0) puts("-1");
else cout<<ans[ex][ey];
return 0;
}