3 solutions
-
0
#include<bits/stdc++.h> using namespace std; const int N=1e2+10; char maze[N][N]; int vis[N][N]; int dirx[]={0,0,-1,1}; int diry[]={-1,1,0,0}; int n,m; struct node1{ int x,y,w; }; queueque; bool check(int x,int y) { if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]0&&maze[x][y]'.') return true; return false; } int bfs() { que.push({0,0,1}); while(!que.empty()) { node1 p=que.front(); que.pop(); if(p.xn-1&&p.ym-1) return p.w ; if(vis[p.x][p.y]) continue; vis[p.x][p.y]=true; for(int i=0;i<4;i++) { int nx=dirx[i]+p.x; int ny=diry[i]+p.y; if(check(nx,ny)) que.push({nx,ny,p.w+1}); } } } int main() { cin>>n>>m; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>maze[i][j]; cout<<bfs()<<endl; return 0; }
Information
- ID
- 769
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 7
- Tags
- # Submissions
- 444
- Accepted
- 87
- Uploaded By