#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++) { for(int j=1; ; j++) { int nx=x+dx[i]*j,ny=y+dy[i]j; if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && maze[nx][ny] != '') { if(dep[nx][ny] == -1){ dep[nx][ny] = dep[x][y] + 1; que.push({nx,ny}); } }else break; } } } cout << dep[n][m]; return 0; }
共 1 条回复
=queue + 链表