fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void fastio() {
  5. ios_base::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7. cout.tie(nullptr);
  8. }
  9.  
  10. static const int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};
  11. static const int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
  12.  
  13. int n;
  14. vector<vector<char>> grid;
  15. vector<vector<bool>> vis;
  16.  
  17. bool valid(int x, int y) {
  18. return (x >= 0 && x < n && y >= 0 && y < n);
  19. }
  20.  
  21. void dfs(int x, int y) {
  22. if (!valid(x, y) || vis[x][y] || grid[x][y] == '0')
  23. return;
  24. vis[x][y] = true;
  25. for (int i = 0; i < 8; i++) {
  26. dfs(x + dx[i], y + dy[i]);
  27. }
  28. }
  29.  
  30. int count_war_eagles() {
  31. int cnt = 0;
  32. for (int i = 0; i < n; i++) {
  33. for (int j = 0; j < n; j++) {
  34. if (grid[i][j] == '1' && !vis[i][j]) {
  35. cnt++;
  36. dfs(i, j);
  37. }
  38. }
  39. }
  40. return cnt;
  41. }
  42.  
  43. void solve() {
  44. int t = 1;
  45. while (cin >> n) {
  46. grid.assign(n, vector<char>(n));
  47. vis.assign(n, vector<bool>(n, false));
  48. for (int i = 0; i < n; i++) {
  49. string line;
  50. cin >> line;
  51. for (int j = 0; j < n; j++)
  52. grid[i][j] = line[j];
  53. }
  54. cout << "Image number " << t++ << " contains " << count_war_eagles() << " war eagles.\n";
  55. }
  56. }
  57.  
  58. int main() {
  59. #ifndef ONLINE_JUDGE
  60. freopen("Bumble.in", "r", stdin);
  61. #endif
  62. fastio();
  63. solve();
  64. return 0;
  65. }
  66.  
Success #stdin #stdout 0.01s 5260KB
stdin
Standard input is empty
stdout
Standard output is empty