fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <algorithm>
  5. #include <stdexcept>
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8.  
  9. class BoardError : public runtime_error {
  10. public:
  11. BoardError(const string& message) : runtime_error(message) {}
  12. };
  13.  
  14. class OOBDrawError : public BoardError {
  15. public:
  16. OOBDrawError(const string& message) : BoardError(message) {}
  17. };
  18.  
  19. class Board {
  20. private:
  21. int m, n;
  22. vector<vector<char>> board;
  23. unordered_map<char, vector<vector<int>>> recordsByPosition;
  24. int priority;
  25.  
  26. vector<vector<int>> getDrawCoordinates(const vector<int>& size, int pos_x, int pos_y) {
  27. if (pos_x + size[0] > m || pos_y + size[1] > n) {
  28. throw OOBDrawError("Size & positions would result in out-of-bounds drawing!");
  29. }
  30.  
  31. vector<vector<int>> coordinates;
  32. for (int i = pos_x; i < pos_x + size[0]; i++) {
  33. for (int j = pos_y; j < pos_y + size[1]; j++) {
  34. coordinates.push_back({i, j});
  35. }
  36. }
  37. return coordinates;
  38. }
  39.  
  40. public:
  41. Board(int m, int n) : m(m), n(n), priority(0), board(m, vector<char>(n, '0')) {}
  42.  
  43. void display() {
  44. cout << '\n';
  45. for (const auto& row : board) {
  46. for (char cell : row) {
  47. cout << cell << " ";
  48. }
  49. cout << '\n';
  50. }
  51. cout << '\n';
  52. }
  53.  
  54. void draw(char c, const vector<int>& size, int pos_x, int pos_y) {
  55. if (recordsByPosition.find(c) != recordsByPosition.end()) {
  56. cout << c << " already exists on the board. Cannot redraw, only move.\n";
  57. return;
  58. }
  59.  
  60. vector<vector<int>> drawCoordinates;
  61. try {
  62. drawCoordinates = getDrawCoordinates(size, pos_x, pos_y);
  63. } catch (const OOBDrawError& e) {
  64. cout << e.what() << '\n';
  65. return;
  66. }
  67.  
  68. for (const auto& coord : drawCoordinates) {
  69. board[coord[0]][coord[1]] = c;
  70. recordsByPosition[c].push_back(coord);
  71. }
  72.  
  73. recordsByPosition[c] = { {pos_x, pos_y} };
  74. }
  75.  
  76. void move(char c, int new_pos_x, int new_pos_y) {
  77. if (recordsByPosition.find(c) == recordsByPosition.end()) {
  78. cout << "Cannot move character " << c << " that does not exist on the board.\n";
  79. return;
  80. }
  81.  
  82. const auto& currPos = recordsByPosition[c];
  83. vector<vector<int>> newCoordinates;
  84. try {
  85. newCoordinates = getDrawCoordinates({1, 1}, new_pos_x, new_pos_y);
  86. } catch (const OOBDrawError& e) {
  87. cout << e.what() << '\n';
  88. return;
  89. }
  90.  
  91. for (const auto& newCoord : newCoordinates) {
  92. if (find(currPos.begin(), currPos.end(), newCoord) == currPos.end()) {
  93. board[newCoord[0]][newCoord[1]] = c;
  94. recordsByPosition[c] = { newCoord };
  95. }
  96. }
  97.  
  98. for (const auto& oldCoord : currPos) {
  99. if (find(newCoordinates.begin(), newCoordinates.end(), oldCoord) == newCoordinates.end()) {
  100. board[oldCoord[0]][oldCoord[1]] = '0';
  101. }
  102. }
  103. }
  104. };
  105.  
  106. int main() {
  107. Board board(4, 5);
  108.  
  109. board.display();
  110. board.draw('a', {2, 2}, 0, 0);
  111. board.display();
  112. board.draw('b', {2, 2}, 0, 1);
  113. board.display();
  114. board.draw('b', {2, 2}, 0, 1);
  115. board.move('a', 0, 1);
  116. board.display();
  117. board.draw('c', {2, 1}, 2, 3);
  118. board.display();
  119. board.move('b', 2, 1);
  120. board.display();
  121. board.move('a', 2, 3);
  122. board.display();
  123. board.move('c', 2, 1);
  124. board.display();
  125. board.move('a', 0, 3);
  126. board.display();
  127. board.move('c', 0, 0);
  128. board.display();
  129.  
  130. return 0;
  131. }
  132.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 


a a 0 0 0 
a a 0 0 0 
0 0 0 0 0 
0 0 0 0 0 


a b b 0 0 
a b b 0 0 
0 0 0 0 0 
0 0 0 0 0 

b already exists on the board. Cannot redraw, only move.

a a b 0 0 
a b b 0 0 
0 0 0 0 0 
0 0 0 0 0 


a a b 0 0 
a b b 0 0 
0 0 0 c 0 
0 0 0 c 0 


a a b 0 0 
a b b 0 0 
0 b 0 c 0 
0 0 0 c 0 


a a b 0 0 
a b b 0 0 
0 b 0 a 0 
0 0 0 c 0 


a a b 0 0 
a b b 0 0 
0 c 0 a 0 
0 0 0 c 0 


a a b a 0 
a b b 0 0 
0 c 0 a 0 
0 0 0 c 0 


c a b a 0 
a b b 0 0 
0 c 0 a 0 
0 0 0 c 0