844走迷宫
作者:
jy9
,
2024-07-17 14:42:45
,
所有人可见
,
阅读 3
大彻大悟了
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 110;
int n, m;
int Map[N][N];
int dis[N][N];
struct node{
int x, y;
};
int main(){
cin >> n >> m;
memset(Map, 1, sizeof(Map));
for (int i = 1; i <= n; i ++ )for(int j = 1; j <= m; j ++)cin >> Map[i][j];
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
queue<node> q;
q.push({1, 1});
while (q.size()){
node now_node = q.front();
q.pop();
for (int i = 0; i < 4; i ++ ){
int x = now_node.x + dx[i];
int y = now_node.y + dy[i];
if(Map[x][y] != 0)continue;
Map[x][y] = 1;
dis[x][y] = dis[now_node.x][now_node.y] + 1;
q.push({x, y});
}
}
cout << dis[n][m];
return 0;
}