fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int N = 10;
  5. int M = 12;
  6. bool field[][12] = {
  7. {1,0,0,0,0,0,0,0,0,1,1,0},
  8. {0,1,1,1,0,0,0,0,0,1,1,1},
  9. {0,0,0,0,1,1,0,0,0,1,1,0},
  10. {0,0,0,0,0,0,0,0,0,1,1,0},
  11. {0,0,0,0,0,0,0,0,0,1,0,0},
  12. {0,0,1,0,0,0,0,0,0,1,0,0},
  13. {0,1,0,1,0,0,0,0,0,1,1,0},
  14. {1,0,1,0,1,0,0,0,0,0,1,0},
  15. {0,1,0,1,0,0,0,0,0,0,1,0},
  16. {0,0,1,0,0,0,0,0,0,0,1,0}
  17. };
  18.  
  19. bool clear(int x, int y) {
  20. if (x < 0 || N <= x || y < 0 || M <= y) return false;
  21. if (field[x][y] == 0) return false;
  22.  
  23. field[x][y] = 0;
  24.  
  25. clear(x - 1, y - 1);
  26. clear(x - 1, y );
  27. clear(x - 1, y + 1);
  28. clear( x , y - 1);
  29. clear( x , y + 1);
  30. clear(x + 1, y - 1);
  31. clear(x + 1, y );
  32. clear(x + 1, y + 1);
  33.  
  34. return true;
  35. }
  36.  
  37. int main() {
  38. int count = 0;
  39.  
  40. for (int i = 0; i < N; i++) {
  41. for (int j = 0; j < M; j++) {
  42. if (clear(i, j)) count++;
  43. }
  44. }
  45.  
  46. cout << count;
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
3