看见讨论少就凑一个讨论
2025-01-23 23:35:10
发布于:广东
4阅读
0回复
0点赞
直接给代码得了
#include <bits/stdc++.h>
using namespace std;
char MAP[49][49];
bool vis[49][49];
struct node {
int x, y, step;
}l,r;
int dir[4][2] = { {-1,0} ,{0,1},{1,0},{0,-1} };
int main() {
int n, m;
cin >> n >> m;
for (int i=1;i<=n;i++) {
cin >> MAP[i]+1;
}
queue<node> q;
q.push({ 1,1,1 });
vis[1][1] = 1;
while (q.size()) {
r = q.front();
q.pop();
if (r.x == n && r.y == m) {
cout << r.step;
break;
}
for (int i = 0; i < 4; i++) {
l.x = r.x + dir[i][0];
l.y = r.y + dir[i][1];
l.step = r.step + 1;
if (l.x >= 1 && l.x <= n && l.y >= 1 && l.y <= m && !vis[l.x][l.y] && MAP[l.x][l.y] == '.') {
vis[l.x][l.y] = 1;
q.push(l);
}
}
}
return 0;
}
这里空空如也
有帮助,赞一个