Advertisement
a53

Castel2

a53
Feb 22nd, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. using namespace std;
  4. const int DIM=1001,
  5. di[]={1,-1,0,0},
  6. dj[]={0,0,1,-1};
  7. int n,m,k,A[DIM][DIM],B[DIM][DIM];
  8.  
  9. int main()
  10. {
  11. ifstream f("castel2.in");
  12. f>>n>>m>>k;
  13. for(int i=1;i<=n;++i)
  14. for(int j=1;j<=m;++j)
  15. {
  16. char x;
  17. f>>x;
  18. if(x=='-')
  19. A[i][j]=0;
  20. if(x=='#')
  21. A[i][j]=-1;
  22. if(x=='Z')
  23. A[i][j]=1;
  24. }
  25. queue<pair<int,int>> Q;
  26. for(int i=1;i<=n;++i)
  27. for(int j=1;j<=m;++j)
  28. if(A[i][j]==1)
  29. Q.push({i,j});
  30. while(!Q.empty())
  31. {
  32. int i=Q.front().first,j=Q.front().second;
  33. Q.pop();
  34. if(A[i][j]<=k)
  35. for(int p=0;p<4;++p)
  36. {
  37. int x=i+di[p],y=j+dj[p];
  38. if(x>0&&x<=n&&y>0&&y<=m&&A[x][y]==0)
  39. A[x][y]=A[i][j]+1,Q.push({x,y});
  40. }
  41. }
  42. Q.push({1,1});
  43. B[1][1]=1;
  44. while(!Q.empty())
  45. {
  46. int i=Q.front().first,j=Q.front().second;
  47. Q.pop();
  48. for(int p=0;p<4;++p)
  49. {
  50. int x=i+di[p],y=j+dj[p];
  51. if(x>0&&x<=n&&y>0&&y<=m&&A[x][y]==0&&B[x][y]==0)
  52. B[x][y]=B[i][j]+1,Q.push({x,y});
  53. }
  54. }
  55. if(B[n][m]==0)
  56. B[n][m]=-1;
  57. ofstream g("castel2.out");
  58. g<< B[n][m];
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement