4 solutions
-
0
#include<bits/stdc++.h> using namespace std; const int N=1e3; char a[N][N]; bool check[N][N]; int m,n,flag=0; 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(int x,int y) { check[x][y]=true; point temp; temp.x=x; temp.y=y; temp.step=0; q.push(temp); while(!q.empty()){ if(a[q.front().x][q.front().y]'*'){ cout<<q.front().step; flag=1; 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<m&&ny>=0&&ny<n){ check[nx][ny]=true; temp.x=nx; temp.y=ny; temp.step=q.front().step+1; q.push(temp); } } q.pop(); } if(flag0){ cout<<"-1"; } } int main() { cin>>m>>n; int i,j,tx,ty; for(i=0;i<m;i++){ for(j=0;j<n;j++){ cin>>a[i][j]; if(a[i][j]=='@'){ tx=i; ty=j; } } } bfs(tx,ty); return 0; }
Information
- ID
- 1236
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 6
- Tags
- # Submissions
- 161
- Accepted
- 50
- Uploaded By