正文
增加 21 行删除 21 行
40 行未改动4142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596974 行未改动40 行未改动using namespace std;const int N = 405;struct P{int x, y, st;};int n, m;int d[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};int vis[N][N];char mp[N][N];void bfs(){memset(vis, 0, sizeof vis);queue<P> q;q.push({1, 1, 1});vis[1][1] = 1;while (q.size()){P now = q.front();q.pop();if (now.x == n && now.y == m){cout << now.st;return ;}for (int i=0; i<4; i++){int nx = now.x + d[i][0];int ny = now.y + d[i][1];if (nx < 1 or nx > n or ny < 1 or ny > m) continue;if (mp[nx][ny] == '#' or vis[nx][ny]) continue;q.push({nx, ny, now.st+1});vis[nx][ny] = 1;}}}int main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin >> n >> m;for (int i=1; i<=n; i++){for (int j=1; j<=m; j++){cin >> mp[i][j];}}4 行未改动40 行未改动4142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596974 行未改动40 行未改动using namespace std;const int N = 405;struct P{int x, y, st;};int n, m;int d[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};int vis[N][N];char mp[N][N];void bfs(){memset(vis, 0, sizeof vis);queue<P> q;q.push({1, 1, 1});vis[1][1] = 1;while (q.size()){P now = q.front();q.pop();if (now.x == n && now.y == m){cout << now.st;return ;}for (int i=0; i<4; i++){int nx = now.x + d[i][0];int ny = now.y + d[i][1];if (nx < 1 or nx > n or ny < 1 or ny > m) continue;if (mp[nx][ny] == '#' or vis[nx][ny]) continue;q.push({nx, ny, now.st+1});vis[nx][ny] = 1;}}}int main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin >> n >> m;for (int i=1; i<=n; i++){for (int j=1; j<=m; j++){cin >> mp[i][j];}}4 行未改动