Advertisement
mickypinata

PROG-T1015: Tiling

Jun 7th, 2021
1,210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef pair<int, int> pii;
  5.  
  6. const int N = 17;
  7.  
  8. pii dir[4] = {pii(0, 1), pii(0, -1), pii(1, 0), pii(-1, 0)};
  9. int board[N + 2][N + 2];
  10.  
  11. int BFS(int r, int c){
  12.     queue<pii> que;
  13.     int tr = board[r][c];
  14.     board[r][c] = 0;
  15.     int cnt = 1;
  16.     que.emplace(r, c);
  17.     while(!que.empty()){
  18.         int ur = que.front().first;
  19.         int uc = que.front().second;
  20.         que.pop();
  21.         for(int d = 0; d < 4; ++d){
  22.             int vr = ur + dir[d].first;
  23.             int vc = uc + dir[d].second;
  24.             if(board[vr][vc] == tr){
  25.                 board[vr][vc] = 0;
  26.                 ++cnt;
  27.                 que.emplace(vr, vc);
  28.             }
  29.         }
  30.     }
  31.     return cnt;
  32. }
  33.  
  34. int main(){
  35.  
  36.     int n;
  37.     scanf("%d", &n);
  38.     for(int i = 1; i <= n; ++i){
  39.         for(int j = 1; j <= n; ++j){
  40.             scanf("%d", &board[i][j]);
  41.         }
  42.     }
  43.  
  44.     int cnt = 0;
  45.     for(int i = 1; i <= n; ++i){
  46.         for(int j = 1; j <= n; ++j){
  47.             if(board[i][j] == 0){
  48.                 continue;
  49.             }
  50.             int tr = board[i][j];
  51.             bool isCandidate = false;
  52.             if(board[i][j + 1] == tr && board[i + 1][j] == tr){
  53.                 isCandidate = true;
  54.             } else if(board[i + 1][j - 1] == tr && board[i + 1][j] == tr){
  55.                 isCandidate = true;
  56.             } else if(board[i][j + 1] == tr && board[i + 1][j + 1] == tr){
  57.                 isCandidate = true;
  58.             } else if(board[i + 1][j] == tr && board[i + 1][j + 1] == tr){
  59.                 isCandidate = true;
  60.             }
  61.             if(BFS(i, j) == 3 && isCandidate){
  62.                 ++cnt;
  63.             }
  64.         }
  65.     }
  66.     cout << cnt;
  67.  
  68.     return 0;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement