Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <stdexcept>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. class mine_sweeper
  9. {
  10. public:
  11. mine_sweeper(const vector<string>& grid)
  12. : m_got_mine(false), m_grid(grid)
  13. {}
  14.  
  15. void stamp(size_t x, size_t y, const char c)
  16. {
  17. if (c == 'x')
  18. {
  19. if (m_grid[x][y] == '*')
  20. {
  21. // bomb
  22. m_got_mine = true;
  23. }
  24. else
  25. {
  26. // do mask (3*3)
  27. do_mask(x, y);
  28. }
  29. }
  30. }
  31.  
  32. const vector<string>& result()
  33. {
  34. if (m_got_mine == false)
  35. {
  36. for (auto &v : m_grid)
  37. {
  38. std::replace(v.begin(), v.end(), '*', '.');
  39. }
  40. }
  41. return m_grid;
  42. }
  43.  
  44. private:
  45. void do_mask(size_t x, size_t y)
  46. {
  47. char nighborhood_mines = '0';
  48. for (int i = -1; i <= 1; i++)
  49. {
  50. for (int j = -1; j <= 1; j++)
  51. {
  52. try
  53. {
  54. if (m_grid.at(x + i).at(y + j) == '*')
  55. {
  56. nighborhood_mines = nighborhood_mines + 1;
  57. }
  58. }
  59. catch (const out_of_range& oor)
  60. {
  61. }
  62. }
  63. }
  64. m_grid[x][y] = nighborhood_mines;
  65. }
  66.  
  67. private:
  68. bool m_got_mine;
  69. vector<string> m_grid;
  70. };
  71.  
  72. void test_case(mine_sweeper &sweeper, const vector<string>& touched_pos)
  73. {
  74. for (size_t rows = 0; rows < touched_pos.size(); rows++)
  75. {
  76. for (size_t columns = 0; columns < touched_pos[rows].size(); columns++)
  77. {
  78. sweeper.stamp(rows, columns, touched_pos[rows][columns]);
  79. }
  80. }
  81. }
  82.  
  83. void output(const vector<string>& minesweeper, size_t testcase)
  84. {
  85. for (auto v : minesweeper)
  86. {
  87. cout << v << endl;
  88. }
  89.  
  90. if (testcase > 0)
  91. {
  92. cout << endl;
  93. }
  94. }
  95.  
  96. int main()
  97. {
  98. size_t testcase = 0;
  99. cin >> testcase;
  100. cin.ignore();
  101.  
  102. while (testcase--)
  103. {
  104. vector<string> minesweeper, touched_pos;
  105. size_t lines = 0;
  106. cin >> lines;
  107. cin.ignore();
  108. for (size_t i = 0; i < lines; i++)
  109. {
  110. string str;
  111. getline(cin, str);
  112. minesweeper.push_back(str);
  113. }
  114.  
  115. mine_sweeper game(minesweeper);
  116.  
  117. for (size_t i = 0; i < lines; i++)
  118. {
  119. string str;
  120. getline(cin, str);
  121. touched_pos.push_back(str);
  122. }
  123.  
  124. test_case(game, touched_pos);
  125.  
  126. output(game.result(), testcase);
  127. }
  128.  
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement