帮@王羲之改的
2024-06-11 13:02:58
发布于:广东
27阅读
0回复
0点赞
嗯嗯嗯额-_-
#include <bits/stdc++.h>
using namespace std;
struct abc {
int x, y, time, hp;
} o, n;
bool vis[15][15];
int mp[15][15];
int w, h;
int sx, sy, ex, ey;
int dir[2][4] = {{0, 1, 0, -1}, {1, 0, -1, 0}};
bool check(abc n) {
return 0 <= n.x && n.x < w && 0 <= n.y && n.y < h && !vis[n.x][n.y] && mp[n.x][n.y] != 0 && n.hp > 0;
}
int main() {
queue<abc> lu;
while (cin >> w >> h) {
memset(vis, 0, sizeof(vis));
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
cin >> mp[i][j];
if (mp[i][j] == 2) {
sx = i;
sy = j;
}
if (mp[i][j] == 3) {
ex = i;
ey = j;
}
}
}
while (!lu.empty()) lu.pop();
lu.push({sx, sy, 0, 6});
while (!lu.empty()) {
o = lu.front();
lu.pop();
if (o.x == ex && o.y == ey) {
cout << o.time << endl;
goto next_test;
}
vis[o.x][o.y] = true;
for (int i = 0; i < 4; i++) {
n.x = o.x + dir[0][i];
n.y = o.y + dir[1][i];
n.time = o.time + 1;
n.hp = o.hp - 1;
if (check(n)) {
if (mp[n.x][n.y] == 4) n.hp = 6;
lu.push(n);
}
}
}
cout << -1 << endl;
next_test:;
}
return 0;
}
这里空空如也
有帮助,赞一个