Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ifstream fin("walle.in");
  4. ofstream fout("walle.out");
  5. struct ceva
  6. {
  7. int x,y;
  8. } p,p1,p2,port[501*501],ex,wally;
  9. queue <ceva> q;
  10. int dx[]= {0, -1, 0, 1, 0};
  11. int dy[]= {0, 0, 1, 0, -1};
  12. int lee[510][510],a[510][510],fol[510][510],n,m,i,j,portalparc,t,nr,trans,k,minim=999999999;
  13. char mat[510][510];
  14. int in(ceva a)
  15. {
  16. return (a.x>=1 && a.x<=n && a.y>=1 && a.y<=m);
  17. }
  18. int cmp(ceva a,ceva b)
  19. {
  20. return lee[a.x][a.y]>lee[b.x][b.y];
  21. }
  22. int main()
  23. {
  24. fin>>n>>m>>t;
  25. for(i=1; i<=n; i++)
  26. for(j=1; j<=m; j++)
  27. {
  28. lee[i][j]=999999999;
  29. a[i][j]=999999999;
  30. fin>>mat[i][j];
  31. if(mat[i][j]=='P')
  32. port[++nr].x=i, port[nr].y=j;
  33. if(mat[i][j]=='W')
  34. wally.x=i, wally.y=j, lee[i][j]=0;
  35. if(mat[i][j]=='E')
  36. ex.x=i, ex.y=j;
  37. }
  38. q.push(ex);
  39. lee[ex.x][ex.y]=0;
  40. while(!q.empty())
  41. {
  42. p1=q.front();
  43. q.pop();
  44. if(mat[p1.x][p1.y]=='+')
  45. trans=t+1;
  46. else trans=1;
  47. for(i=1; i<=4; i++)
  48. {
  49. p2.x=p1.x+dx[i];
  50. p2.y=p1.y+dy[i];
  51. if(in(p2) && lee[p2.x][p2.y] > lee[p1.x][p1.y]+trans && mat[p2.x][p2.y]!='#')
  52. {
  53. lee[p2.x][p2.y]=lee[p1.x][p1.y]+trans;
  54. q.push(p2);
  55. }
  56. }
  57. }
  58. sort(port+1,port+nr+1,cmp);
  59. a[wally.x][wally.y]=0;
  60. q.push({wally.x,wally.y});
  61. while(!q.empty())
  62. {
  63. minim=min(minim,a[ex.x][ex.y]);
  64. p1=q.front();
  65. q.pop();
  66. if(mat[p1.x][p1.y]=='+')
  67. trans=t+1;
  68. else trans=1;
  69. for(i=1; i<=4; i++)
  70. {
  71. p2.x=p1.x+dx[i];
  72. p2.y=p1.y+dy[i];
  73. if(mat[p1.x][p1.y]==mat[p2.x][p2.y] && mat[p1.x][p1.y]=='P')
  74. continue;
  75. if(in(p2) && mat[p2.x][p2.y]!='#' && a[p2.x][p2.y] > a[p1.x][p1.y] + trans)
  76. {
  77. a[p2.x][p2.y]=a[p1.x][p1.y]+trans;
  78. if(mat[p2.x][p2.y]!='P')
  79. q.push(p2);
  80. else
  81. {
  82. if(port[1].x==p2.x && port[1].y==p2.y)
  83. {
  84. a[port[2].x][port[2].y]=a[p2.x][p2.y];
  85. fol[port[2].x][port[2].y]++;
  86. q.push(port[2]);
  87. }
  88. else
  89. {
  90. a[port[1].x][port[1].y]=a[p2.x][p2.y];
  91. fol[port[1].x][port[1].y]++;
  92. q.push(port[1]);
  93. }
  94. }
  95. }
  96. else if(in(p2) && mat[p2.x][p2.y]=='P' && fol[p2.x][p2.y]<=nr)
  97. {
  98. if(port[1].x==p2.x && port[1].y==p2.y && fol[port[2].x][port[2].y]<=nr)
  99. {
  100. a[port[2].x][port[2].y]=a[p1.x][p1.y]+trans;
  101. fol[port[2].x][port[2].y]++;
  102. q.push(port[2]);
  103. }
  104. else if(fol[port[1].x][port[1].y]<=nr)
  105. {
  106. a[port[1].x][port[1].y]=a[p1.x][p1.y]+trans;
  107. fol[port[1].x][port[1].y]++;
  108. q.push(port[1]);
  109. }
  110. }
  111. }
  112. }
  113. if(minim==999999999)
  114. fout<<-1;
  115. else fout<<minim;
  116. return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement