RT,代码如下
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug cout<<"weifeng_yuhuo's debug\n";
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const ll INF=0x3f3f3f3f;
struct pos{
ll x,y;
};
ll n,m,e,sx,sy,fx,fy;
ll dx[13]={0,-1,-2,-3,0,0,0,1,2,3,0,0,0};
ll dy[13]={0,0,0,0,-1,-2,-3,0,0,0,1,2,3};
ll a[55][55],dis[55][55],facesave[55][55];
char first_face;
queue<pos>q;
void bfs(){
if(first_face=='N')facesave[sx][sy]=1;
if(first_face=='W')facesave[sx][sy]=2;
if(first_face=='S')facesave[sx][sy]=3;
if(first_face=='E')facesave[sx][sy]=4;
q.push({sx,sy});
dis[sx][sy]=0;
while(!q.empty()){
ll x=q.front().x;
ll y=q.front().y;
q.pop();
for(ll i=1;i<=12;i++){
ll xx=x+dx[i],yy=y+dy[i];
if(2<=xx&&xx<=n&&2<=yy&&yy<=m&&a[xx][yy]==0){
ll new_face=(i+2)/3,excnt;
if(facesave[x][y]==new_face)excnt=0;
else if((facesave[x][y]+new_face)&1)excnt=1;
else excnt=2;
if((dis[x][y]+excnt+1<dis[xx][yy])||((dis[x][y]+excnt+1==dis[xx][yy])&&(new_face!=facesave[xx][yy]))){
q.push({xx,yy});
dis[xx][yy]=dis[x][y]+excnt+1;
facesave[xx][yy]=new_face;
}
}
}
}
}
signed main()
{
IOS;
cin>>n>>m;
for(ll i=1;i<=n;i++){
for(ll j=1;j<=m;j++){
cin>>e;
if(e)a[i][j]=a[i+1][j]=a[i][j+1]=a[i+1][j+1]=1;
}
}
cin>>sx>>sy>>fx>>fy>>first_face;
sx++;sy++;fx++;fy++;
for(ll i=1;i<=n+1;i++)for(ll j=1;j<=m+1;j++)dis[i][j]=INF;
bfs();
if(dis[fx][fy]==INF)cout<<-1;
else cout<<dis[fx][fy];
return 0;
}