广搜,但是用数组
2024-03-14 13:32:17
发布于:广东
13阅读
0回复
0点赞
#include <iostream>
#include <cstdio>
//#include <queue>
using namespace std;
struct Point{
int x, y, step;
}a[100005];
int n, m, ct;
bool vis[45][45];
char mp[45][45];
int dir[4][2] = {-1, 0, 0, -1, 0, 1, 1, 0};
bool check(int x, int y){
if(x < 1 || x > n) return 0;
if(y < 1 || y > m) return 0;
if(mp[x][y] == '#') return 0;
if(vis[x][y]) return 0;
return 1;
}
int bfs(int x, int y){
vis[x][y] = 1;
a[++ct] = {x, y, 0};
for(int i = 1; i <= ct; i++){
if(a[i].x == n && a[i].y == m){
return a[i].step;
}
for(int j = 0; j < 4; j++){
int xx = a[i].x + dir[j][0], yy = a[i].y + dir[j][1];
if(check(xx, yy)){
a[++ct] = {xx, yy, a[i].step + 1};
vis[xx][yy] = 1;
}
}
}return -1;
}
int main(){
freopen("guangsou.in", "r", stdin);
freopen("guangsou.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> mp[i][j];
}
}cout << bfs(1, 1);
fclose(stdin), fclose(stdout);
return 0;
}
全部评论 1
6
2024-03-14 来自 广东
0
有帮助,赞一个