fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int n;
  5. string grid[30];
  6.  
  7. int dx[] = {1, 1, 1, 0, -1, -1, -1, 0};
  8. int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
  9.  
  10. void display() {
  11. for(int i = 0; i < n; i++)
  12. cout << grid[i] << endl;
  13. cout << endl;
  14. }
  15.  
  16. void ff(int x, int y) {
  17. grid[x][y] = '2';
  18. display();
  19. for(int i = 0; i < 8; i++) {
  20. int xx = x+dx[i];
  21. int yy = y+dy[i];
  22. if(xx >= 0 && xx < n
  23. && yy >= 0 && yy < n
  24. && grid[xx][yy] == '1')
  25. ff(xx, yy);
  26. }
  27. // (x+1, y), (x+1,y+1), ...
  28. }
  29.  
  30. int main() {
  31. // review: bfs vs dfs
  32. // review: representasi graph dalam coding
  33. // literal vs implicit graph
  34. // 1. adjacency matrix
  35. /*
  36. int adjMat[100][100];
  37. 1 2 3 4 5 6
  38. 1 0 1 0 1 0 0
  39. 2 1 0 1 1 0 0
  40. 3 0 1 0 0 1 0
  41. 4 1 1 0 0 1 0
  42. 5 0 0 1 1 0 1
  43. 6 0 0 0 0 1 0
  44. */
  45. // 2. adjacency list
  46. /*
  47. vector<int> adjList[100];
  48. 1: 2, 4
  49. 2: 1, 3, 4
  50. 3: 2, 5
  51. 4: 1, 2, 5
  52. 5: 3, 4, 6
  53. 6: 5
  54. */
  55. // 3. edge list
  56. /*
  57. vector<pair<int, int> > edgeList;
  58. (1, 2)
  59. (1, 4)
  60. (2, 1)
  61. (2, 3)
  62. (2, 4)
  63. ...
  64. */
  65.  
  66. // flood fill
  67. int casenum = 1;
  68. while(cin >> n) {
  69. for(int i = 0; i < n; i++)
  70. cin >> grid[i];
  71. int counter = 0;
  72. for(int i = 0; i < n; i++)
  73. for(int j = 0; j < n; j++)
  74. if(grid[i][j] == '1') {
  75. counter++;
  76. ff(i, j);
  77. }
  78. cout << "Image number " << casenum++ << " contains " << counter << " war eagles." << endl;
  79. }
  80. return 0;
  81. }
Success #stdin #stdout 0.01s 5284KB
stdin
6
100100
001010
000000
110000
111000
010100
8
01100101
01000001
00011000
00000010
11000011
10100010
10000001
01100000
stdout
200100
001010
000000
110000
111000
010100

200200
001010
000000
110000
111000
010100

200200
002010
000000
110000
111000
010100

200200
002020
000000
110000
111000
010100

200200
002020
000000
210000
111000
010100

200200
002020
000000
210000
211000
010100

200200
002020
000000
210000
211000
020100

200200
002020
000000
210000
212000
020100

200200
002020
000000
210000
212000
020200

200200
002020
000000
220000
212000
020200

200200
002020
000000
220000
222000
020200

Image number 1 contains 3 war eagles.
02100101
01000001
00011000
00000010
11000011
10100010
10000001
01100000

02100101
02000001
00011000
00000010
11000011
10100010
10000001
01100000

02200101
02000001
00011000
00000010
11000011
10100010
10000001
01100000

02200201
02000001
00011000
00000010
11000011
10100010
10000001
01100000

02200202
02000001
00011000
00000010
11000011
10100010
10000001
01100000

02200202
02000002
00011000
00000010
11000011
10100010
10000001
01100000

02200202
02000002
00021000
00000010
11000011
10100010
10000001
01100000

02200202
02000002
00022000
00000010
11000011
10100010
10000001
01100000

02200202
02000002
00022000
00000020
11000011
10100010
10000001
01100000

02200202
02000002
00022000
00000020
11000021
10100010
10000001
01100000

02200202
02000002
00022000
00000020
11000021
10100020
10000001
01100000

02200202
02000002
00022000
00000020
11000021
10100020
10000002
01100000

02200202
02000002
00022000
00000020
11000022
10100020
10000002
01100000

02200202
02000002
00022000
00000020
21000022
10100020
10000002
01100000

02200202
02000002
00022000
00000020
21000022
20100020
10000002
01100000

02200202
02000002
00022000
00000020
21000022
20100020
20000002
01100000

02200202
02000002
00022000
00000020
21000022
20100020
20000002
02100000

02200202
02000002
00022000
00000020
21000022
20100020
20000002
02200000

02200202
02000002
00022000
00000020
22000022
20100020
20000002
02200000

02200202
02000002
00022000
00000020
22000022
20200020
20000002
02200000

Image number 2 contains 6 war eagles.