p352.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <algorithm>
  2. #include <array>
  3. #include <iostream>
  4. #include <map>
  5. #include <utility>
  6. #include <vector>
  7. #include <iomanip>
  8. #include <sstream>
  9. #include <climits>
  10. #include <queue>
  11. #include <list>
  12. using namespace std;
  13. typedef unsigned int nat;
  14. void flood_fill(vector<vector<nat>> &bitmap, nat x, nat y, nat color = 0)
  15. {
  16. nat original_color = bitmap[x][y];
  17. //cout << "Flood_fill(" << x << ", " << y << ", " << color << ") original color = " << original_color << endl;
  18. queue<pair<int,int>> bf;
  19. bf.push({x, y});
  20. while(!bf.empty())
  21. {
  22. //cout << "Size: " << bf.size() << endl;
  23. pair<int,int> tile = bf.front();
  24. bf.pop();
  25. bitmap[tile.first][tile.second] = color;
  26. for(int i = tile.second-1; i <= tile.second+1; ++i)
  27. {
  28. for(int j = tile.first-1; j <= tile.first+1; ++j)
  29. {
  30. if(x == 3 && y == 0)
  31. {
  32. //cout << "checking x: " << j << " y: " << i << endl;
  33. }
  34. if(i >= 0 && i < bitmap.size() && j >= 0 && j < bitmap[0].size() && bitmap[j][i] == original_color)
  35. {
  36. //cout << j << ", " << i << " : " << bitmap[j][i] << " <= " << color << endl;
  37. bf.push({j,i});
  38. bitmap[j][i] = color;
  39. //cout << "Dude wtf?!" << j << ", " << i << " : " << bitmap[j][i] << " == " << color << endl;
  40. }
  41. }
  42. }
  43. }
  44. }
  45. void print_grid(const vector<vector<nat>> &bitmap)
  46. {
  47. for(auto row : bitmap)
  48. {
  49. for(auto cell : row)
  50. {
  51. cout << cell;
  52. }
  53. cout << endl;
  54. }
  55. }
  56. int main()
  57. {
  58. nat grid_size, num = 1;
  59. while(cin >> grid_size)
  60. {
  61. cin.ignore(1000,'\n');
  62. vector<vector<nat>> grid(grid_size, vector<nat>(grid_size, 0));
  63. nat num_eagles = 0;
  64. for(nat y = 0; y < grid_size; ++y)
  65. {
  66. string str;
  67. getline(cin, str);
  68. for(nat x = 0; x < grid_size; ++x)
  69. {
  70. if(str[x] == '1')
  71. grid[y][x] = 1;
  72. }
  73. }
  74. //print_grid(grid);
  75. //cout << endl;
  76. for(nat y = 0; y < grid_size; ++y)
  77. {
  78. for(nat x = 0; x < grid_size; ++x)
  79. {
  80. if(grid[x][y] == 1)
  81. {
  82. ++num_eagles;
  83. flood_fill(grid, x, y, 0);
  84. //print_grid(grid);
  85. //cout << endl;
  86. }
  87. }
  88. }
  89. cout << "Image number " << num++ << " contains " << num_eagles << " war eagles." << endl;
  90. }
  91. return 0;
  92. }