fork download
  1. #include<bits/stdc++.h>
  2. #define fr(i,n) for(int i=1;i<=n;i++)
  3. #define xx first
  4. #define yy second
  5. using namespace std;
  6. int n;
  7.  
  8. int dx[]={1,-1,0,0,-1,-1,1,1};
  9. int dy[]={0,0,-1,1,-1,1,-1,1};
  10. #define pii pair<int,int>
  11. bool vis[100][100];
  12. char g[100][100];
  13.  
  14. void bfs(int sx,int sy)
  15. {
  16. vis[sx][sy]=1;
  17. queue<pii>q;
  18. q.push(pii(sx,sy));
  19. while(!q.empty())
  20. {
  21. pii u=q.front();
  22. q.pop();
  23. pii t;
  24. for(int i=0;i<8;i++){
  25. t.xx=u.xx+dx[i];
  26. t.yy=u.yy+dy[i];
  27. if(t.xx>=1 &&t.xx<=n &&t.yy>=1 &&t.yy<=n &&g[t.xx][t.yy]=='1' &&!vis[t.xx][t.yy])
  28. {
  29. vis[t.xx][t.yy]=1;
  30. q.push(pii(t.xx,t.yy));
  31. }
  32. }
  33. }
  34.  
  35. }
  36.  
  37.  
  38.  
  39. int main() {
  40.  
  41. int cs=1;
  42. while(cin>>n && n){
  43.  
  44. memset(vis,0,sizeof vis);
  45.  
  46. fr(i,n) fr(j,n)
  47. cin>>g[i][j];
  48. int co=0;
  49. fr(i,n) fr(j,n)
  50. if(g[i][j]=='1' &&vis[i][j]==0)
  51. {
  52. bfs(i,j);
  53. co++;
  54. }
  55. printf("Image number %d contains %d war eagles.\n",cs++,co);
  56.  
  57. }
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0s 3496KB
stdin
1
1
21
000101001000011100100
011011100100100000101
100010011010111000010
000001111010011000100
101100111110000110011
010010011001011000100
100011010101100001110
011110000000001100110
000001010011110110110
110000111110010110111
010001000111101010101
101010000101001111001
111110110101000001001
100111011110000011101
000010110110100001100
100110000001000001110
100101111101000000000
101111100110010101011
111010000111010100111
110100000101101101110
011001011000011111111
17
01110001111011001
10110101000111110
01110001011010111
10011110011001010
11011110100101000
11111100100010101
11001101001001000
11110110100001101
11101100011011100
00110011110010011
00000011000110110
00011111001010011
01100000111111111
10101101101110110
11000111000011110
11110011110100010
01100000000110000
15
001000010011001
101100100000000
011111110111011
101100110100101
000111000011100
110000010100100
001010111110000
010100100110110
010101110011110
101111000001010
110100101100110
110001001001111
000011001101011
000000101001000
100100000110111
5
11010
00011
11111
01011
00101
23
10000100101100011101111
10111100010111100110110
11010111010111010111000
01111011101101011111001
10001011100011100101111
00111100100101010000110
01101110111111110101110
11011001011000111000111
11111001111001001011010
10101010111000011100000
01011101000011000111111
01101101100011001010000
00111011000011100100010
01011011000010100100011
01011001110000011011110
01100101110010101000110
00001111110011000101010
10010100000010101001111
00101111001001011111010
11110010011100011001111
00011110001101000111101
01010101110001101110011
01000001011110001100111
2
01
11
11
00110110111
00011000100
10010101011
10010001111
00000101111
10100101000
01011100001
11101010110
11000000011
00010111000
10100111100
1
0
12
110101100001
011010100110
000001110110
100110010111
100011010000
111100111100
001010111110
110110111001
101011010111
110001110100
111001111011
010000111001
12
110010111110
000111101101
010010001000
011100110101
011001101010
011111111101
110010111111
010111010101
010100101100
010101110101
000010100110
100000100110
12
011100101101
001110000101
001111101011
111001111101
111101001110
100010101001
010101010110
010111001000
010110110110
010011010011
111111100010
001000100111
3
100
010
101
12
010010110011
010011101111
101011110111
101100001011
101010001110
001010000111
101000101100
101000000111
111000110011
111001010100
100101010110
001011100000
2
01
00
15
010010011011111
010110110111111
010001001000111
000111111000110
001001011110101
001011010110100
100100001010000
101110011000010
010001101001101
100011110010111
100000111001110
100111000001010
110011000100101
110101001001101
110011101111001
15
101000001001100
100111010111111
111010100001101
111101000011011
100001011100010
111101011010011
101110011001001
000110011111110
101010001000100
000011110000111
000111111011011
111100111111111
111100010011100
110000111010001
110000111111001
8
10100110
01101100
10111000
11110001
01110010
00111100
11001010
00101011
12
100110111110
110010101100
001010001000
010001010010
100010000101
000100111110
011101111111
101111010000
001010000110
001010100010
111100000101
001000000101
18
110100101001001011
010011001101000011
001011111110010011
111100010011100001
110100101111100011
010001011001111101
100111001011110100
100000011100000011
110111010000110100
101010010001010001
100111000010100001
011011110111101100
000101111000101010
100001110000110100
110010010111010010
101101110110000001
010000001000000110
010101101101100111
stdout
Image number 1 contains 1 war eagles.
Image number 2 contains 3 war eagles.
Image number 3 contains 5 war eagles.
Image number 4 contains 6 war eagles.
Image number 5 contains 2 war eagles.
Image number 6 contains 3 war eagles.
Image number 7 contains 1 war eagles.
Image number 8 contains 8 war eagles.
Image number 9 contains 0 war eagles.
Image number 10 contains 2 war eagles.
Image number 11 contains 3 war eagles.
Image number 12 contains 2 war eagles.
Image number 13 contains 1 war eagles.
Image number 14 contains 2 war eagles.
Image number 15 contains 1 war eagles.
Image number 16 contains 3 war eagles.
Image number 17 contains 3 war eagles.
Image number 18 contains 1 war eagles.
Image number 19 contains 5 war eagles.
Image number 20 contains 6 war eagles.