#include<iostream>
#include<queue>
using namespace std;
int n,m,sx,sy,tx,ty;
char fang;
bool vis[105][105],flag=true;
struct path
{
int x,y,f,step;
};
queue<path>q;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int f;
scanf("%d",&f);
if(f==1)
{
vis[i-1][j]=true;
vis[i][j-1]=true;
vis[i-1][j-1]=true;
vis[i][j]=true;
}
}
}
scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
if(sx==tx&&sy==ty)
{
printf("0");
return 0;
}
cin>>fang;
switch(fang)
{
case'N':q.push({sx,sy,1,0});
case'S':q.push({sx,sy,3,0});
case'W':q.push({sx,sy,4,0});
case'E':q.push({sx,sy,2,0});
}
while(!q.empty())
{
path a=q.front();
q.pop();
if(a.x<=0||a.y<=0||a.x>=n||a.y>=m)
{
continue;
}
if(vis[a.x][a.y])
{
continue;
}
if(a.x==tx&&a.y==ty)
{
flag=false;
printf("%d",a.step);
break;
}
switch(a.f)
{
case 1:q.push({a.x-1,a.y,a.f,a.step+1});
q.push({a.x-2,a.y,a.f,a.step+1});
q.push({a.x-3,a.y,a.f,a.step+1});
break;
case 2:q.push({a.x,a.y+1,a.f,a.step+1});
q.push({a.x,a.y+2,a.f,a.step+1});
q.push({a.x,a.y+3,a.f,a.step+1});
break;
case 3:q.push({a.x+1,a.y,a.f,a.step+1});
q.push({a.x+2,a.y,a.f,a.step+1});
q.push({a.x+3,a.y,a.f,a.step+1});
break;
case 4:q.push({a.x,a.y-1,a.f,a.step+1});
q.push({a.x,a.y-2,a.f,a.step+1});
q.push({a.x,a.y-3,a.f,a.step+1});
break;
}
if(a.f==4)q.push({a.x,a.y,1,a.step+1});
else q.push({a.x,a.y,a.f+1,a.step+1});
if(a.f==1)q.push({a.x,a.y,4,a.step+1});
else q.push({a.x,a.y,a.f-1,a.step+1});
}
if(flag)
{
printf("%d",-1);
}
return 0;
}
测试数据