Advertisement
SuitNdtie

PROG-1015: Tiling

May 16th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<queue>
  3. using namespace std;
  4. int n;
  5.  
  6. int board[20][20];
  7. bool visited[20][20];
  8.  
  9. void clearvisited(){
  10.     for(int i = 0 ; i < 20 ; i ++)for(int j = 0 ; j < 20 ; j ++)visited[i][j] = false;
  11. }
  12. struct pos{
  13.     int I,J;
  14. };
  15.  
  16. int moveI[4] = {-1,0,1,0};
  17. int moveJ[4] = {0,1,0,-1};
  18.  
  19. bool IsInB(int I,int N){
  20.     return (0 <= I && I < N);
  21. }
  22. int cntcom(int I,int J){
  23.     int key = board[I][J];
  24.     queue<pos> q;
  25.     clearvisited();
  26.     int cnt = 0;
  27.     q.push({I,J});
  28.     while(!q.empty()){
  29.         int uI = q.front().I;
  30.         int uJ = q.front().J;
  31.         q.pop();
  32.         if(visited[uI][uJ])continue;
  33.         cnt ++;
  34.         visited[uI][uJ] = true;
  35.         for(int i = 0 ; i < 4 ; i ++){
  36.             int vI = uI + moveI[i];
  37.             int vJ = uJ + moveJ[i];
  38.             if(IsInB(vI,n) && IsInB(vJ,n) && !visited[vI][vJ] && board[vI][vJ] == key){
  39.                 q.push({vI,vJ});
  40.             }
  41.         }
  42.     }
  43.     return cnt;
  44. }
  45. void erase(int I,int J){
  46.     int key = board[I][J];
  47.     queue<pos> q;
  48.     clearvisited();
  49.     q.push({I,J});
  50.     while(!q.empty()){
  51.         int uI = q.front().I;
  52.         int uJ = q.front().J;
  53.         q.pop();
  54.         if(visited[uI][uJ])continue;
  55.         board[uI][uJ] = 0;
  56.         visited[uI][uJ] = true;
  57.         for(int i = 0 ; i < 4 ; i ++){
  58.             int vI = uI + moveI[i];
  59.             int vJ = uJ + moveJ[i];
  60.             if(IsInB(vI,n) && IsInB(vJ,n) && !visited[vI][vJ] && board[vI][vJ] == key){
  61.                 q.push({vI,vJ});
  62.             }
  63.         }
  64.     }
  65. }
  66. bool minicheck(int I,int J){
  67.     int key = board[I][J];
  68.    
  69.     int TypeA1I[4] = {0,-1,0,-1};
  70.     int TypeA1J[4] = {1,0,-1,0};
  71.    
  72.     int TypeA2I[4] = {1,0,1,0};
  73.     int TypeA2J[4] = {0,-1,0,1};
  74.    
  75.     for(int i = 0 ; i < 4 ; i ++){
  76.         pos A1;
  77.         pos A2;
  78.         A1.I = I + TypeA1I[i];
  79.         A1.J = J + TypeA1J[i];
  80.         A2.I = I + TypeA2I[i];
  81.         A2.J = J + TypeA2J[i];
  82.        
  83.         if(IsInB(A1.I,n) && IsInB(A1.J,n) && IsInB(A2.I,n) && IsInB(A2.J,n)){
  84.             if(board[I][J] == board[A1.I][A1.J] && board[I][J] == board[A2.I][A2.J])return true;
  85.         }
  86.     }
  87.     return false;
  88. }
  89. int check(int I,int J){
  90.     int key = board[I][J];
  91.     queue<pos> q;
  92.     clearvisited();
  93.     q.push({I,J});
  94.    
  95.     pos arr[3];
  96.     int idx = 0;
  97.     while(!q.empty()){
  98.         int uI = q.front().I;
  99.         int uJ = q.front().J;
  100.         q.pop();
  101.         if(visited[uI][uJ])continue;
  102.         arr[idx++] = {uI,uJ};
  103.         visited[uI][uJ] = true;
  104.         for(int i = 0 ; i < 4 ; i ++){
  105.             int vI = uI + moveI[i];
  106.             int vJ = uJ + moveJ[i];
  107.             if(IsInB(vI,n) && IsInB(vJ,n) && !visited[vI][vJ] && board[vI][vJ] == key){
  108.                 q.push({vI,vJ});
  109.             }
  110.         }
  111.     }
  112.    
  113.     for(int i = 0 ; i < 3 ; i ++){
  114.         if(minicheck(arr[i].I,arr[i].J))return 1;
  115.     }
  116.     return 0;
  117. }
  118.  
  119. int main()
  120. {
  121.     scanf("%d",&n);
  122.     for(int i = 0 ; i < n ; i ++){
  123.         for(int j = 0 ; j < n ; j ++){
  124.             scanf("%d",&board[i][j]);
  125.         }
  126.     }
  127.     int ans = 0;
  128.     for(int i = 0 ; i < n ; i ++){
  129.         for(int j = 0 ; j < n ; j ++){
  130.             if(board[i][j] == 0)continue;
  131.             if(cntcom(i,j) != 3){
  132.                 erase(i,j);
  133.                 continue;
  134.             }
  135.             else{
  136.                 ans += check(i,j);
  137.                 erase(i,j);
  138.             }
  139.         }
  140.     }
  141.     printf("%d",ans);
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement