2 solutions
-
0
#include<bits/stdc++.h> using namespace std; const int N=1e2+10; char a[N][N]; bool check[N][N]; int m,n,tx,ty; struct point{ int x; int y; int step; }; int dx[4]={0,-1,0,1}; int dy[4]={-1,0,1,0}; queueq; void bfs() { check[tx][ty]=true; point temp; temp.x=tx; temp.y=ty; temp.step=0; q.push(temp); while(!q.empty()){ if(a[q.front().x][q.front().y]'T'){ cout<<q.front().step; break; } for(int i=0;i<4;i++){ int nx=dx[i]+q.front().x; int ny=dy[i]+q.front().y; if(a[nx][ny]!='#'&&!check[nx][ny]&&nx>=0&&nx<n&&ny>=0&&ny<m){ check[nx][ny]=true; temp.x=nx; temp.y=ny; temp.step=q.front().step+1; q.push(temp); } } q.pop(); } } int main() { cin>>n>>m; int i,j; for(i=0;i<n;i++){ for(j=0;j<m;j++){ cin>>a[i][j]; if(a[i][j]'S'){ tx=i; ty=j; } } } bfs(); return 0; }
Information
- ID
- 770
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 6
- Tags
- # Submissions
- 191
- Accepted
- 53
- Uploaded By