#include <bits/stdc++.h> using namespace std;
const int N = 1000 + 10; const int dx[]= {0,0,-1,1}; const int dy[]= {-1,1,0,0}; struct Node { int x,y; }; char maze[N][N]; int dep[N][N]; queue que; int main() { int n, m; cin >> n >> m; for (int i=1; i<=n; i++) { for (int j=1; j<=m; j++) cin >> maze[i][j]; } memset(dep,-1,sizeof(dep)); dep[1][1] = 0; que.push({1,1}); while(!que.empty()) { int x=que.front().x,y=que.front().y; que.pop(); for(int i=0; i<4; i++) { int nx=x+dx[i],ny=y+dy[i]; if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && maze[nx][ny] != '*' && dep[nx][ny] == -1) { dep[nx][ny] = dep[x][y] + 1; que.push({nx,ny}); } } } cout << dep[n][m]; return 0; }
共 3 条回复
代码框:I SUN YOUR MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER'S MOTHER