SuitNdtie

Space

Jun 2nd, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<queue>
  3. #include<algorithm>
  4. using namespace std;
  5. struct pos{
  6.     int I,J;
  7.     pos operator + (const pos& rhs)const
  8.     {
  9.         return {I + rhs.I , J + rhs.J};
  10.     }
  11. };
  12.  
  13. int n,m;
  14. bool bound(pos a){
  15.     return (0 <= a.I && a.I < n && 0 <= a.J && a.J < m);
  16. }
  17.  
  18.  
  19. int main()
  20. {
  21.     scanf("%d %d",&m,&n);
  22. //  if(m > 1000)return -1; 
  23.     char arr[n][m+1];
  24.     for(int i = 0 ; i < n ; i ++){
  25.         scanf(" %s",arr[i]);
  26.     }
  27. /*  for(int i = 0 ; i < n ; i ++){
  28.         for(int j = 0 ; j < m ; j ++){
  29.             printf("%c",arr[i][j]);
  30.         }
  31.         printf("\n");
  32.     }
  33.     return 0;
  34. */  int a = 0,b = 0 ,c = 0;
  35.     for(int i = 0 ; i < n ; i ++){
  36.         for(int j = 0 ; j < m ; j ++){
  37.             if(arr[i][j] == '0')continue;
  38.            
  39.             queue<pos> q;
  40.             q.push({i,j});
  41.             int mini = 10010;
  42.             int minj = 10010;
  43.             int maxi = -1;
  44.             int maxj = -1;
  45.             int cnt = 0;
  46.             while(!q.empty()){
  47.                 pos u = q.front();
  48.                 q.pop();
  49.                 if(arr[u.I][u.J] == '0')continue;
  50.                 arr[u.I][u.J] = '0';
  51.                 mini = min(mini,u.I);
  52.                 minj = min(minj,u.J);
  53.                 maxi = max(maxi,u.I);
  54.                 maxj = max(maxj,u.J);
  55.                 cnt ++;
  56.                 pos move[4] = {{-1,0},{0,1},{1,0},{0,-1}};
  57.                 for(int k = 0 ; k < 4 ; k ++){
  58.                     pos v = u + move[k];
  59.                     if(bound(v) && arr[v.I][v.J] == '1'){
  60.                         q.push(v);
  61.                     }
  62.                 }
  63.             }
  64.         //  printf("Test (%d,%d) : %d (%d,%d) , (%d,%d)\n",i,j,cnt,mini,minj,maxi,maxj);
  65.             if(maxi - mini == maxj - minj){
  66.                 int len = maxi - mini + 1;
  67.                 if(cnt == len*len){
  68.                     a++;
  69.                 }
  70.                 else{
  71.                     b++;
  72.                 }
  73.             }
  74.             else{
  75.                 c++;
  76.             }
  77.         }
  78.     }
  79.     printf("%d %d %d",a,b,c);
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment