deadwing97

ROLLBAR Editorialist

Feb 24th, 2019
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MX = (1009);
  6.  
  7. int T , n , m;
  8. string valid[MX];
  9. int sx , sy;
  10. int bfs[MX][MX][3];
  11.  
  12. queue < pair < pair < int , int > , int > >  Q;
  13.  
  14.  
  15. bool check(int x , int y){
  16.     if(x < 1 || x > n || y < 1 || y > m) return 0;
  17.     if(valid[x][y] == '0') return 0;
  18.     return 1;
  19. }
  20.  
  21. void add(int nx , int ny , int nstate , int ncost){
  22.     if(bfs[nx][ny][nstate] != -1) return;
  23.     bfs[nx][ny][nstate] = ncost;
  24.     Q.push({{nx , ny},nstate});
  25. }
  26.  
  27. void relax(int x , int y , int state){
  28.     int C = bfs[x][y][state];
  29.  
  30.     if(state == 0){
  31.  
  32.         if(check(x , y - 1) && check(x , y - 2)) add(x , y - 2 , 1 , C + 1);
  33.         if(check(x , y + 1) && check(x , y + 2)) add(x , y + 1 , 1 , C + 1);
  34.  
  35.         if(check(x - 1 , y) && check(x - 2 , y)) add(x - 2 , y , 2 , C + 1);
  36.         if(check(x + 1 , y) && check(x + 2 , y)) add(x + 1 , y , 2 , C + 1);
  37.     }
  38.  
  39.     if(state == 1){
  40.  
  41.         if(check(x - 1 , y) && check(x - 1 , y + 1)) add(x - 1 , y , 1 , C + 1);
  42.         if(check(x + 1 , y) && check(x + 1 , y + 1)) add(x + 1 , y , 1 , C + 1);
  43.  
  44.         if(check(x , y + 2)) add(x , y + 2 , 0 , C + 1);
  45.         if(check(x , y - 1)) add(x , y - 1 , 0 , C + 1);
  46.  
  47.     }
  48.  
  49.     if(state == 2){
  50.  
  51.  
  52.         if(check(x , y - 1) && check(x + 1 , y - 1)) add(x , y - 1 , 2 , C + 1);
  53.         if(check(x , y + 1) && check(x + 1 , y + 1)) add(x , y + 1 , 2 , C + 1);
  54.  
  55.  
  56.         if(check(x + 2 , y)) add(x + 2 , y , 0 , C + 1);
  57.         if(check(x - 1 , y)) add(x - 1 , y , 0 , C + 1);
  58.  
  59.     }
  60.  
  61.  
  62. }
  63. int main(){
  64.  
  65.   //  freopen("in.in","r",stdin);
  66.     ios_base::sync_with_stdio(0);
  67.     cin.tie(0); cout.tie(0);
  68.  
  69.     cin>>T;
  70.  
  71.     while(T--){
  72.  
  73.         memset(bfs , -1 , sizeof(bfs));
  74.  
  75.         cin>>n>>m>>sx>>sy;
  76.  
  77.         for(int j = 1 ; j <= n ; j++){ cin>>valid[j]; valid[j] = "#" + valid[j]; };
  78.  
  79.  
  80.         bfs[sx][sy][0] = 0;
  81.         Q.push({{sx , sy} , 0});
  82.  
  83.         while(!Q.empty()){
  84.  
  85.             int x = Q.front().first.first , y = Q.front().first.second , state = Q.front().second; Q.pop();
  86.  
  87.             relax(x , y , state);
  88.  
  89.         }
  90.  
  91.         for(int j = 1 ; j <= n ; j++){
  92.             for(int i = 1 ; i <= m ; i++){
  93.                 cout<<bfs[j][i][0]<<' ';
  94.             }
  95.             cout<<endl;
  96.         }
  97.  
  98.  
  99.     }
  100.  
  101.  
  102.  
  103.  
  104. }
Add Comment
Please, Sign In to add comment