Advertisement
Mohammad_Dipu_Sultan

Endoscope_SRBD

Oct 15th, 2023
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n, m, ans;
  5. int a[100][100];
  6. int seen[100][100];
  7. bool valid(int x, int y){
  8.     return (x >= 0 and x < n and y >= 0 and y<m);
  9. }
  10.  
  11. int right(int x, int y){
  12.     if(valid(x, y)==true){
  13.         int k = a[x][y];
  14.         if(k==1 or k==3 or k==4 or k==5){
  15.             return true;
  16.         }
  17.     }
  18.     return false;
  19. }
  20. int left(int x, int y){
  21.     if(valid(x, y)==true){
  22.         int k = a[x][y];
  23.         if(k==1 or k==3 or k==6 or k==7){
  24.             return true;
  25.         }
  26.     }
  27.     return false;
  28. }
  29. int up(int x, int y){
  30.     if(valid(x, y)==true){
  31.         int k = a[x][y];
  32.         if(k==1 or k==2 or k==4 or k==7){
  33.             return true;
  34.         }
  35.     }
  36.     return false;
  37. }
  38. int down(int x, int y){
  39.     if(valid(x, y)==true){
  40.         int k = a[x][y];
  41.         if(k==1 or k==2 or k==5 or k==6){
  42.             return true;
  43.         }
  44.     }
  45.     return false;
  46. }
  47. void solve(int x, int y, int l){
  48.     if(l == 0){
  49.         return;
  50.     }
  51.  
  52.     if(seen[x][y]==0){
  53.         seen[x][y] = 1;
  54.         ans++;
  55.     }
  56.  
  57.     if(right(x, y) and left(x, y+1)){
  58.         solve(x, y+1, l-1);
  59.     }
  60.     if(left(x, y) and right(x, y-1)){
  61.         solve(x, y-1, l-1);
  62.     }
  63.     if(up(x, y) and down(x-1, y)){
  64.         solve(x-1, y, l-1);
  65.     }
  66.     if(down(x, y) and up(x+1, y)){
  67.         solve(x+1, y, l-1);
  68.     }
  69. }
  70. int main(){
  71.  
  72.     int t;
  73.     cin >> t;
  74.     while(t--){
  75.         int x, y, l;
  76.         cin >> n >> m >> x >> y >> l;
  77.         for(int i = 0; i < n; i++){
  78.             for(int j = 0; j < m; j++){
  79.                 cin >> a[i][j];
  80.                 seen[i][j] = 0;
  81.             }
  82.         }
  83.  
  84.         ans = 0;
  85.         solve(x, y, l);
  86.         cout << ans << endl;
  87.     }
  88.  
  89.     return 0;
  90. }
  91.  
  92. /*2
  93. 5 6 2 1 3
  94. 0 0 5 3 6 0
  95. 0 0 2 0 2 0
  96. 3 3 1 3 7 0
  97. 0 0 0 0 0 0
  98. 0 0 0 0 0 0
  99. 5 6 2 2 6
  100. 3 0 0 0 0 3
  101. 2 0 0 0 0 6
  102. 1 3 1 1 3 1
  103. 2 0 2 0 0 2
  104. 0 0 4 3 1 1
  105.  
  106. 5
  107. 15
  108. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement