shalivitalya

Задача 4

Mar 25th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1e3+5;
  6.  
  7. int n, m, ans, ks, kb, xx, yy, x, y;
  8. bool used[N][N];
  9. int M[N][N];
  10.  
  11. void dfs(int x, int y){
  12.     if(x<0 || y<0 ||x>=n || y>=m || M[x][y] ==2|| used[x][y]){ return; }
  13.     used[x][y] = 1;
  14.     if(M[x][y]==1) --ans;
  15.     for(int i = -1; i < 2; ++i){
  16.         for(int j = -1; j < 2; ++j){
  17.             if(i && j) continue;
  18.             dfs(x+i,y+j);
  19.         }
  20.     }
  21. }
  22.  
  23. int main(){
  24.     ios_base::sync_with_stdio(0);
  25.     cin.tie(0); cout.tie(0);
  26.     cin >> n >> m >> ks >> kb >> xx >> yy;
  27.     ans = kb;
  28.     for(int i = 0; i < ks; ++i){
  29.         cin >> x >> y;
  30.         M[x][y] = 2;
  31.     }
  32.     for(int i = 0; i < kb; ++i){
  33.         cin >> x >> y;
  34.         M[x][y] = 1;
  35.     }
  36.     dfs(yy,xx);
  37.     cout << ans;
  38.  
  39.     return 0;
  40. }
Add Comment
Please, Sign In to add comment