Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <queue>
- using namespace std;
- const int DIM=1001,
- di[]={1,-1,0,0},
- dj[]={0,0,1,-1};
- int n,m,k,A[DIM][DIM],B[DIM][DIM];
- int main()
- {
- ifstream f("castel2.in");
- f>>n>>m>>k;
- for(int i=1;i<=n;++i)
- for(int j=1;j<=m;++j)
- {
- char x;
- f>>x;
- if(x=='-')
- A[i][j]=0;
- if(x=='#')
- A[i][j]=-1;
- if(x=='Z')
- A[i][j]=1;
- }
- queue<pair<int,int>> Q;
- for(int i=1;i<=n;++i)
- for(int j=1;j<=m;++j)
- if(A[i][j]==1)
- Q.push({i,j});
- while(!Q.empty())
- {
- int i=Q.front().first,j=Q.front().second;
- Q.pop();
- if(A[i][j]<=k)
- for(int p=0;p<4;++p)
- {
- int x=i+di[p],y=j+dj[p];
- if(x>0&&x<=n&&y>0&&y<=m&&A[x][y]==0)
- A[x][y]=A[i][j]+1,Q.push({x,y});
- }
- }
- Q.push({1,1});
- B[1][1]=1;
- while(!Q.empty())
- {
- int i=Q.front().first,j=Q.front().second;
- Q.pop();
- for(int p=0;p<4;++p)
- {
- int x=i+di[p],y=j+dj[p];
- if(x>0&&x<=n&&y>0&&y<=m&&A[x][y]==0&&B[x][y]==0)
- B[x][y]=B[i][j]+1,Q.push({x,y});
- }
- }
- if(B[n][m]==0)
- B[n][m]=-1;
- ofstream g("castel2.out");
- g<< B[n][m];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement