#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using PII = pair<int,int>;
using ll = long long;
using ull = unsigned long long;
int a[60][60];
int vis[60][60][5];
int n,m;
int op[] = {1,2,3,4,5};
int rx[] = {2,0,3,1};
int lx[] = {1,3,0,2};
// 0 N
//1 2 w e
// 3 s
int res = 0x3f3f3f3f;
int cx[] = {0,1,1,0};
int cy[] = {0,0,1,1};
struct rob{
int x,y,to;
};
int sx,sy,ex,ey;
char sto;
int check(int x,int y){
if(x < 1 || x > n-1 || y < 1 || y > m-1)return 0;
for(int i=0;i<4;i++){
int xx = x + cx[i];
int yy = y + cy[i];
if(a[xx][yy] == 1) return 0;
}
return 1;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
cin>>sx>>sy>>ex>>ey>>sto;
int a;
if(sto == 'N') a = 0;
else if (sto == 'S') a = 3;
else if(sto == 'W') a = 1;
else if(sto == 'E')a = 2;
queue<rob> q;
q.push({sx,sy,a});
vis[sx][sy][a] = 1;
while(q.size()){
auto u = q.front();
q.pop();
if(u.x == ex && u.y == ey){
//break;
cout<<vis[u.x][u.y][u.to]-1;
return 0;
}
int fail = 0;
for(int i=0;i<5;i++){
int x = u.x;
int y = u.y;
int to = u.to;
if(i < 3 && fail == 0){
switch (to) {
case 0: x-=op[i];break;
case 1: y-=op[i];break;
case 2: y+=op[i];break;
case 3: x+=op[i];break;
}
if(check(x,y) == 0) {
fail = 1;
continue;
}
if(!vis[x][y][to]){
q.push({x,y,to});
vis[x][y][to] = vis[u.x][u.y][u.to] + 1;
}
}else if(i==3){
to = rx[u.to];
if(!vis[x][y][to]){
q.push({x,y,to});
vis[x][y][to] = vis[u.x][u.y][u.to] + 1;
}
}else if(i==4){
to = lx[u.to];
if(!vis[x][y][to]){
q.push({x,y,to});
vis[x][y][to] = vis[u.x][u.y][u.to] + 1;
}
}
}
}
cout<<-1;
return 0;
}
大模拟,甩写一百行
有一个特殊情况就是如果走一步撞墙了,那么后续走2,3步都否决掉,直接转向