fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int n, map[25][25], cnt = 0;
  8. int dx[] = {0, 0, 1, -1};
  9. int dy[] = {1, -1, 0, 0};
  10. vector<int> ans;
  11.  
  12. void dfs(int y, int x)
  13. {
  14. cnt++;
  15. map[y][x] = 0;
  16.  
  17. for(int i = 0; i < 4; i++)
  18. {
  19. int nx = x + dx[i];
  20. int ny = y + dy[i];
  21.  
  22. if(0 <= nx && nx < n && 0 <= ny && ny < n)
  23. {
  24. if(map[ny][nx])
  25. {
  26. dfs(ny, nx);
  27. }
  28. }
  29. }
  30. }
  31.  
  32. int main(int argc, const char *argv[])
  33. {
  34. cin >> n;
  35. for(int i = 0; i < n; i++)
  36. {
  37. for(int j = 0; j < n; j++)
  38. {
  39. scanf("%ld", &map[i][j]);
  40. }
  41. }
  42.  
  43. for(int i = 0; i < n; i++)
  44. {
  45. for(int j = 0; j < n; j++)
  46. {
  47. if(map[i][j])
  48. {
  49. cnt = 0;
  50. dfs(i, j);
  51. ans.push_back(cnt);
  52. }
  53. }
  54. }
  55.  
  56. sort(ans.begin(), ans.end());
  57.  
  58. cout << ans.size() << "\n";
  59. for(int i = 0; i < ans.size(); i++)
  60. {
  61. cout << ans[i] << "\n";
  62. }
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0s 4440KB
stdin
7
0110100
0110101
1110101
0000111
0100000
0111110
0111000
stdout
1
7