Advertisement
Guest User

Untitled

a guest
Feb 7th, 2021
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  5. #define endl "\n"
  6. #define int long long
  7.  
  8. int dx[] = {1, -1, 0, 0};
  9. int dy[] = {0, 0, 1, -1};
  10.  
  11. const int N = 1005;
  12. int n, m, k;
  13. char a[N][N];
  14. int vis[N][N];
  15. int dis[N][N];
  16. int final[N][N];
  17. pair<int, int> src, des;
  18.  
  19. int32_t main()
  20. {
  21. IOS
  22. cin >> n >> m >> k;
  23. for(int i = 1; i <= n; i++) {
  24. for(int j = 1; j <= m; j++) {
  25. dis[i][j] = -1;
  26. }
  27. }
  28. for(int i = 1; i <= n; i++) {
  29. for(int j = 1; j <= m; j++) {
  30. cin >> a[i][j];
  31. if(a[i][j] == 'S') {
  32. src = {i, j};
  33. }
  34. if(a[i][j] == 'E') {
  35. des = {i, j};
  36. }
  37. if(a[i][j] == '#')
  38. dis[i][j] = 0;
  39. }
  40. }
  41. priority_queue<tuple<int, int, int>> q;
  42. for(int i = 1; i <= k; i++) {
  43. int r, c, d;
  44. cin >> r >> c >> d;
  45. q.push({d, r, c});
  46. vis[r][c] = 1;
  47. dis[r][c] = d;
  48. }
  49. while(!q.empty()) {
  50. int d, x, y;
  51. tie(d, x, y) = q.top();
  52. q.pop();
  53. for(int i = 0; i < 4; i++) {
  54. int nx = x + dx[i];
  55. int ny = y + dy[i];
  56. if(nx == 0 || nx > n || ny == 0 || ny > m || a[nx][ny] == '#')
  57. continue;
  58. if(!vis[nx][ny]) {
  59. dis[nx][ny] = dis[x][y] - 1;
  60. q.push({dis[nx][ny], nx, ny});
  61. vis[nx][ny] = 1;
  62. }
  63. }
  64. }
  65. for(int i = 1; i <= n; i++) {
  66. for(int j = 1; j <= m; j++) {
  67. vis[i][j] = 0;
  68. final[i][j] = 1e9;
  69. }
  70. }
  71. if(dis[src.first][src.second] >= 0) {
  72. cout << "IMPOSSIBLE" << endl;
  73. exit(0);
  74. }
  75. queue<pair<int, int>> pq;
  76. pq.push(src);
  77. final[src.first][src.second] = 0;
  78. while(!pq.empty()) {
  79. pair<int, int> tmp = pq.front();
  80. int x = tmp.first;
  81. int y = tmp.second;
  82. pq.pop();
  83. for(int i = 0; i < 4; i++) {
  84. int nx = x + dx[i];
  85. int ny = y + dy[i];
  86. if(nx == 0 || nx > n || ny == 0 || ny > m || dis[nx][ny] >= 0)
  87. continue;
  88. if(!vis[nx][ny]) {
  89. final[nx][ny] = final[x][y] + 1;
  90. pq.push({nx, ny});
  91. vis[nx][ny] = 1;
  92. }
  93. }
  94. }
  95. if(final[des.first][des.second] == 1e9) {
  96. cout << "IMPOSSIBLE" << endl;
  97. } else {
  98. cout << final[des.first][des.second] << endl;
  99. }
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement