fork download
  1. #include <cstdio>
  2. #include <queue>
  3. using namespace std;
  4. int visited[625]={0}, n, map[25][25]={0, },ans,cnt=0;
  5. const int row[4]= {0, 0 , -1 ,1};
  6. const int col[4]= {1, -1, 0 , 0};
  7. queue <pair<int, int> > q;
  8. void bfs(int r, int c)
  9. {
  10. cnt++;
  11. q.push(pair<int, int> (r, c)); ans=0 ;
  12. while(!q.empty())
  13. {
  14. int R= q.front().first;
  15. int C= q.front().second;
  16. q.pop();
  17. for(int i=0 ; i <4; i++)
  18. {
  19. int nr= R+row[i];
  20. int nc= C+col[i];
  21. if(nr<0 || nr>=n || nc<0 || nc>=n) continue;
  22. if(map[nr][nc]==1){
  23. q.push(pair<int, int>(nr, nc)); map[nr][nc]=map[R][C]+1; ans++;
  24. }
  25. }
  26. }
  27. if(ans==0) ans=1;
  28. visited[ans]++;
  29. }
  30. int main()
  31. {
  32. scanf("%d", &n);
  33. for(int i= 0 ; i< n; i++)
  34. for(int j = 0 ; j < n ; j ++) scanf("%d", &map[i][j]);
  35. for(int i= 0 ; i< n; i++)
  36. for(int j = 0 ; j < n ; j ++) if(map[i][j]==1) bfs(i, j);
  37. printf("%d\n", cnt);
  38. for(int i =0 ; i< n*n ; i++)
  39. {
  40. if(visited[i])
  41. {
  42. while(visited[i]--)
  43. printf("%d\n", i);
  44. }
  45. }
  46. }
  47.  
Success #stdin #stdout 0s 4276KB
stdin
7
0110100
0110101
1110101
0000111
0100000
0111110
0111000
stdout
0