文章目录
#include <bits/stdc++.h>
using namespace std;
int a[100][100], v[100][100] = {0};
struct point{
int x;
int y;
int step;
};
queue<point> r;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main(){
int h, w, startx, starty, p, q;
cin >> h >> w;
startx = starty = 1;
p = h;
q = w;
for(int i = 1; i <= h; i ++){
for(int j = 1; j <= w; j ++){
cin >> a[i][j];
}
}
point start;
start.x = startx;
start.y = starty;
start.step = 0;
r.push(start);
v[startx][starty] = 1;
while(!r.empty()){
int x = r.front().x, y = r.front().y;
if(x == p && y == q){
cout << r.front().step;
break;
}
for(int i = 0; i < 4; i ++){
int tx, ty;
tx = x + dx[i];
ty = y + dy[i];
if(a[tx][ty] == 0 && v[tx][ty] == 0){
point temp;
temp.x = tx;
temp.y = ty;
temp.step = r.front().step + 1;
r.push(temp);
v[tx][ty] = 1;
}
}
r.pop();
}
return 0;
}