Guest User

Untitled

a guest
Feb 6th, 2021
150
264 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define F first
  5. #define S second
  6. #define pii pair<int,int>
  7. #define all(a) a.begin(),a.end()
  8. const int N = 1e3 + 5;
  9. char grid[N][N];
  10. bool vis[N][N];
  11. int dist[N][N], n, m, k, dir[4][2] = {{1,0}, {-1,0}, {0,1}, {0,-1}};
  12. bool isvalid(int x, int y){
  13.     if(x >= 1 && y >= 1 && x <= n && y <= m && grid[x][y] != '#'){
  14.         return 1;
  15.     }
  16.     return 0;
  17. }
  18. void fillit(){
  19.     for(int i = 0; i < N; i++)
  20.         for(int j = 0; j < N; j++)
  21.             vis[i][j] = 0, dist[i][j] = 0;
  22. }
  23. void bfs1(int x, int y, int d){
  24.     vis[x][y] = 1;
  25.     dist[x][y] = 0;
  26.     queue<pii> q;
  27.     q.push({x,y});
  28.     while(!q.empty()){
  29.         auto cur = q.front();
  30.         q.pop();
  31.         if(dist[cur.F][cur.S] <= d){
  32.             grid[cur.F][cur.S] = '#';
  33.         }
  34.         else continue;
  35.         for(int i = 0; i < 4; i++){
  36.             int nx = cur.F + dir[i][0], ny = cur.S + dir[i][1];
  37.             if(vis[nx][ny]) continue;
  38.             if(isvalid(nx,ny)){
  39.                 vis[nx][ny] = 1;
  40.                 q.push({nx,ny});
  41.                 dist[nx][ny] = 1 + dist[cur.F][cur.S];
  42.             }
  43.         }
  44.     }
  45. }
  46. void bfs(int x, int y){
  47.     queue<pii> q;
  48.     q.push({x,y});
  49.     dist[x][y] = 0;
  50.     vis[x][y] = 1;
  51.     while(!q.empty()){
  52.         auto cur = q.front();
  53.         q.pop();
  54.         for(int i = 0; i < 4; i++){
  55.             int nx = cur.F + dir[i][0], ny = cur.S + dir[i][1];
  56.             if(vis[nx][ny]) continue;
  57.             if(isvalid(nx,ny)){
  58.                 vis[nx][ny] = 1;
  59.                 q.push({nx,ny});
  60.                 dist[nx][ny] = 1 + dist[cur.F][cur.S];
  61.             }
  62.         }
  63.     }
  64. }
  65. void solve(){
  66.     cin >> n >> m >> k;
  67.     int sx, sy, ex, ey;
  68.     for(int i = 1; i <= n; i++){
  69.         for(int j = 1; j <= m; j++){
  70.             cin >> grid[i][j];
  71.             if(grid[i][j] == 'S')
  72.                 sx = i, sy = j;
  73.             if(grid[i][j] == 'E')
  74.                 ex = i, ey = j;
  75.         }
  76.     }
  77.     for(int i = 0; i < k;  i++){
  78.         int a,b,c; cin >> a >> b >> c;
  79.         fillit();
  80.         bfs1(a,b,c);
  81.     }
  82. //  bfs1();
  83.    
  84.     fillit();
  85.     if(grid[ex][ey] == '#' || grid[sx][sy] == '#'){
  86.         cout << "IMPOSSIBLE";
  87.         return;
  88.     }
  89.     bfs(sx,sy);
  90.     if(dist[ex][ey] == 0){
  91.         cout << "IMPOSSIBLE";
  92.         return;
  93.     }
  94.     else cout << dist[ex][ey];
  95. }
  96. int32_t main(){
  97.     ios::sync_with_stdio(false);
  98.     /*#ifndef ONLINE_JUDGE
  99.     freopen("input.txt", "r", stdin);freopen("answer.txt", "w", stdout);
  100.     #endif */
  101.     int t = 1;
  102.     //cin >> t;
  103.     while(t--){
  104.         solve();
  105.     }    
  106. }
  107.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×