1 solutions
-
0
#include<bits/stdc++.h> using namespace std; char f[505][505]; int ma[505][505]; int n,m,c,flag=1; int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; struct grid{ int x,y; }; void bfs(int x,int y){ queue<grid>q; q.push((grid){x,y}); while(!q.empty()){ int xx=q.front().x; int yy=q.front().y; q.pop(); for(int i=0;i<4;i++){ int xxx=xx+dx[i]; int yyy=yy+dy[i]; if(xxx>0&&xxx<=n&&yyy>0&&yyy<=m&&f[xxx][yyy]=='.'){ ma[xxx][yyy]=ma[xx][yy]+1; f[xxx][yyy]='#'; q.push((grid){xxx,yyy}); } else if(f[xxx][yyy]=='E'){ flag=0; ma[xxx][yyy]=ma[xx][yy]+1; } } } return ; } int main(){ cin>>n>>m>>c; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>f[i][j]; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(f[i][j]=='S'){ bfs(i,j); } } } if(flag){ cout<<-1; return 0; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(f[i][j]=='E'){ cout<<ma[i][j]*c; } } } return 0; }
Information
- ID
- 1445
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 8
- Tags
- # Submissions
- 13
- Accepted
- 8
- Uploaded By