第8个样例答案是33,我输出是34
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
typedef pair<PII, char> PIIC;
const int N = 60;
int n, m;
int a[N][N], d[N][N];
int sx, sy, ex, ey;
bool st[N][N];
int dx[4][3] = {{-1, -2, -3}, {0, 0, 0}, {1, 2, 3}, {0, 0, 0}};
int dy[4][3] = {{0, 0, 0}, {1, 2, 3}, {0, 0, 0}, {-1, -2, -3}};
char s[6] = {'N', 'E', 'S', 'W'};
int ddx[4] = {-1, -1, 0, 0}, ddy[4] = {-1, 0, -1, 0};
bool check(int x, int y)
{
for(int i = 0; i < 4; i ++)
{
int ax = x + ddx[i], ay = y + ddy[i];
if(ax < 0 || ay < 0 || ax >= n || ay >= m || a[ax][ay])
return false;
}
return true;
}
int bfs(char op)
{
queue<PIIC> q;
q.push({{sx, sy}, op});
st[sx][sy] = 1;
while(q.size())
{
auto [p, f] = q.front(); q.pop();
int ax = p.first, ay = p.second;
if(ax == ex && ay == ey) return d[ax][ay];
for(int i = 0; i < 4; i ++)
for(int j = 0; j < 3; j ++)
{
int x = ax + dx[i][j], y = ay + dy[i][j];
if(x < 0 || y < 0 || x >= n || y >= m || !check(x, y)) break;
if(!st[x][y])
{
d[x][y] = d[ax][ay] + 1;
if(s[i] != f) d[x][y] ++;
st[x][y] = 1;
q.push({{x, y}, s[i]});
}
}
}
return -1;
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
cin >> a[i][j];
cin >> sx >> sy >> ex >> ey;
char op;
cin >> op;
cout << bfs(op) << endl;
return 0;
}