fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <cassert>
  5. #include <queue>
  6.  
  7. typedef std::vector<char> column;
  8. typedef std::vector<column> gamefield;
  9. typedef std::pair<int, int> index;
  10.  
  11. std::istream& operator>>(std::istream& in, gamefield &gf)
  12. {
  13. std::vector<std::string> lines;
  14. for (std::string s; std::getline(in, s); assert(s.size() == lines.front().size()))
  15. lines.push_back(s);
  16.  
  17. if (lines.empty())
  18. in.setstate(std::ios::failbit);
  19. gf.resize(2+lines.front().size(), std::vector<char>(1, '\0'));
  20. gf.front() = gf.back() = std::vector<char>(2+lines.size(), '\0');
  21.  
  22. for (int y=lines.size()-1; y>=0; --y)
  23. for (int x=0; x<lines.front().size(); ++x)
  24. gf[1+x].push_back(lines[y][x]);
  25.  
  26. return in;
  27. }
  28.  
  29. std::ostream& operator<<(std::ostream& out, gamefield const& gf)
  30. {
  31. assert(!gf.empty());
  32. for (int x=gf.front().size()-2; x>0; --x, out << '\n')
  33. for (int y=1; y<gf.size()-1; ++y)
  34. out << (gf[y][x] ? gf[y][x] : '-');
  35. return out;
  36. }
  37.  
  38. void pop(gamefield& gf, index idx)
  39. {
  40. char color_to_remove = gf[idx.first][idx.second];
  41. std::queue<index> q;
  42. q.push(idx);
  43. while (!q.empty()) {
  44. int y = q.front().first;
  45. int x = q.front().second;
  46. q.pop();
  47.  
  48. gf[y][x] = '\0';
  49. #undef check
  50. #define check(dy, dx) /* ein macro ist hier einfachsten */ \
  51.   if (gf[y+dy][x+dx] == color_to_remove) q.push(std::make_pair(y+dy, x+dx))
  52.  
  53. check(+1, -1); check(+1, 0); check(+1, +1);
  54. check( 0, -1); check( 0, +1);
  55. check(-1, -1); check(-1, 0); check(-1, +1);
  56. #undef check
  57. }
  58.  
  59. for (int i=1; i<gf.size()-1; ++i)
  60. std::fill(std::remove(gf[i].begin()+1, gf[i].end(), '\0'), gf[i].end(), '\0');
  61. }
  62.  
  63. int main()
  64. {
  65. gamefield gf;
  66. std::cin >> gf;
  67. std::cout << gf << '\n';
  68.  
  69. pop(gf, std::make_pair(1, 1));
  70.  
  71. std::cout << gf << '\n';
  72. }
  73.  
Success #stdin #stdout 0.02s 2828KB
stdin
abcdexxxxxx
12345x6789x
abcdexfxghx
12345x6xxxx
abcdexxfghi
xxxxxxxxxxx
stdout
abcdexxxxxx
12345x6789x
abcdexfxghx
12345x6xxxx
abcdexxfghi
xxxxxxxxxxx

-----------
abcde------
12345------
abcde-6-89-
12345-f7gh-
abcde-6fghi