求助大佬,只有70分
#include <bits/stdc++.h>
using namespace std;
int maze[1001][1001], n, m;
struct node {
int x, y, pre, stp;
/*
* x, y : 坐标
* pre : 上一步是走的哪里,避免重复走,只记录一步
*/
node () {
}
node (int _x, int _y, int _pre, int _stp) {
x = _x;
y = _y;
pre = _pre;
stp = _stp;
}
};
bool operator < (const node& a, const node& b) {
if (a.x != b.x) {
return a.x < b.x;
} else if (a.x == b.x && a.y != b.y) {
return a.y < b.y;
} else if (a.x == b.x && a.y != b.y && a.pre != b.pre) {
return a.pre < b.pre;
} else if (a.x == b.x && a.y != b.y && a.pre == b.pre && a.stp != b.stp) {
return a.stp < b.stp;
}
}
map <node, bool > inq;
queue <node > q;
inline bool canGoThere(node st) {
return st.x >= 1 && st.x <= n && st.y >= 1 && st.y <= m && !inq[st];
}
inline void solve() {
q.push(node(1, 1, -1, 0));
inq[node(1, 1, -1, 0)] = 1;
while (!q.empty()) {
node now = q.front();
q.pop();
// cout << now.x << ' ' << now.y << ' ' << now.pre << ' ' << now.stp << endl;
if (now.x == n && now.y == m) {
cout << now.stp << endl;
exit(0);
}
if (canGoThere(node(now.x + maze[now.x][now.y], now.y, 1, now.stp + 1)) && now.pre != 1) {
q.push(node(now.x + maze[now.x][now.y], now.y, 1, now.stp + 1));
inq[node(now.x + maze[now.x][now.y], now.y, 1, now.stp + 1)] = 1;
}
if (canGoThere(node(now.x - maze[now.x][now.y], now.y, 2, now.stp + 1)) && now.pre != 2) {
q.push(node(now.x - maze[now.x][now.y], now.y, 2, now.stp + 1));
inq[node(now.x - maze[now.x][now.y], now.y, 2, now.stp + 1)] = 1;
}
if (canGoThere(node(now.x, now.y + maze[now.x][now.y], 3, now.stp + 1)) && now.pre != 3) {
q.push(node(now.x, now.y + maze[now.x][now.y], 3, now.stp + 1));
inq[node(now.x, now.y + maze[now.x][now.y], 3, now.stp + 1)] = 1;
}
if (canGoThere(node(now.x, now.y - maze[now.x][now.y], 4, now.stp + 1)) && now.pre != 4) {
q.push(node(now.x, now.y - maze[now.x][now.y], 4, now.stp + 1));
inq[node(now.x, now.y - maze[now.x][now.y], 4, now.stp + 1)] = 1;
}
if (canGoThere(node(now.x - maze[now.x][now.y], now.y - maze[now.x][now.y], 5, now.stp + 1)) && now.pre != 5) {
q.push(node(now.x - maze[now.x][now.y], now.y - maze[now.x][now.y], 5, now.stp + 1));
inq[node(now.x - maze[now.x][now.y], now.y - maze[now.x][now.y], 5, now.stp + 1)] = 1;
}
if (canGoThere(node(now.x + maze[now.x][now.y], now.y - maze[now.x][now.y], 6, now.stp + 1)) && now.pre != 6) {
q.push(node(now.x + maze[now.x][now.y], now.y - maze[now.x][now.y], 6, now.stp + 1));
inq[node(now.x + maze[now.x][now.y], now.y - maze[now.x][now.y], 6, now.stp + 1)] = 1;
}
if (canGoThere(node(now.x - maze[now.x][now.y], now.y + maze[now.x][now.y], 7, now.stp + 1)) && now.pre != 7) {
q.push(node(now.x - maze[now.x][now.y], now.y + maze[now.x][now.y], 7, now.stp + 1));
inq[node(now.x - maze[now.x][now.y], now.y + maze[now.x][now.y], 7, now.stp + 1)] = 1;
}
if (canGoThere(node(now.x + maze[now.x][now.y], now.y + maze[now.x][now.y], 8, now.stp + 1)) && now.pre != 8) {
q.push(node(now.x + maze[now.x][now.y], now.y + maze[now.x][now.y], 8, now.stp + 1));
inq[node(now.x + maze[now.x][now.y], now.y + maze[now.x][now.y], 8, now.stp + 1)] = 1;
}
}
}
int main() {
ios::sync_with_stdio(0);
cin >> m >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> maze[i][j];
}
}
/*for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cout << maze[i][j] << ' ';
}
cout << endl;
}*/
solve();
cout << "NEVER" << endl;
}