#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
struct Node{
int x,y,qwq;
char c;
Node()=default;
explicit Node(int x,int y,char c,int qwq) : x(x),y(y),c(c),qwq(qwq) {}
};
int change(char c){
if (c=='E'){
return 1;
}
if (c=='S'){
return 2;
}
if (c=='W'){
return 3;
}
return 4;
}
int main(){
int n,m;
cin >> n >> m;
vector<vector<int>> maze(n+10,vector<int>(m+10,0));
vector<vector<vector<bool>>> st(n+10,vector<vector<bool>>(n+10,vector<bool>(5,false)));
int t;
for (int i=1;i<=n;++i){
for (int j=1;j<=m;++j){
scanf("%d",&t);
if (t==1){
maze[i][j]=maze[i][j+1]=maze[i+1][j]=maze[i+1][j+1]=1;
}
}
}
++n,++m;
unordered_map<char,pair<char,char>> m1;
unordered_map<char,pair<int,int>> m2;
m1['E']={'N','S'},m1['W']={'N','S'},m1['N']={'E','W'},m1['S']={'E','W'};
m2['N']={-1,0},m2['S']={1,0},m2['E']={0,1},m2['W']={0,-1};
queue<Node> q;
int x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
char c;
cin >> c;
q.emplace(x1+1,y1+1,c,0);
st[x1+1][y1+1][change(c)]=true;
++x2,++y2;
++n,++m;
int x,y,qwq,tx,ty,i;
while (!q.empty()){
x=q.front().x,y=q.front().y,qwq=q.front().qwq;
c=q.front().c;
q.pop();
if (x==x2 && y==y2){
cout<<qwq<<endl;
return 0;
}
++qwq;
if (st[x][y][change(m1[c].first)]==false){
q.emplace(x,y,m1[c].first,qwq);
st[x][y][change(m1[c].first)]=true;
}
if (st[x][y][change(m1[c].second)]==false){
q.emplace(x,y,m1[c].second,qwq);
st[x][y][change(m1[c].second)]=true;
}
q.emplace(x,y,m1[c].second,qwq);//转弯
for (i=1;i<=3;++i){//直走
tx=x+m2[c].first*i,ty=y+m2[c].second*i;
if (tx<=0 || tx>n || ty<=0 || ty>m){
continue;
}
if (maze[tx][ty]==1){
break;
}
if (st[tx][ty][change(c)]==false){
q.emplace(tx,ty,c,qwq);
st[tx][ty][change(c)]=true;
}
}
}
cout<<-1<<endl;
return 0;
}
#2 是
4 4
0 0 0 0
0 1 0 0
0 0 0 0
0 0 0 0
1 1 2 2 S
答案应该是
-1
但是 T 了