P1031 魔鬼之城 帮忙看一下,WA 70了,请大佬们帮我查查错,非常感谢!
#include<bits/stdc++.h>
using namespace std;
struct point{
int x, y, step, last;
};
queue<point> q;
int dx[10] = {0, 0, 0, 1, -1, 1, -1, 1, -1};
int dy[10] = {0, 1, -1, 0, 0, 1, 1, -1, -1};
int n, m, g[105][105], used[105][105];
int main()
{
cin >> m >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d", &g[i][j]);
q.push(point{1, 1, 0, 0});
used[1][1] = 1;
while(!q.empty())
{
point u = q.front(); q.pop();
point v;
for(int i = 1; i <= 8; i++)
{
if(u.last == i) continue;
v.x = u.x + dx[i] * g[u.x][u.y];
v.y = u.y + dy[i] * g[u.x][u.y];
if(v.x < 1 || v.x > n || v.y < 1 || v.y > m) continue;
if(used[v.x][v.y] == 1) continue;
v.step = u.step + 1, v.last = i;
q.push(v);
used[v.x][v.y] = 1;
if(v.x == n && v.y == m)
{
cout << v.step << endl;
return 0;
}
}
}
cout << "NEVER" << endl;
return 0;
}