2/28/2013

Surrounded Regions

Surrounded RegionsFeb 22
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region .
For example,
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
Idea: BFS

  1. class Solution {
  2. public:
  3. void solve(vector<vector<char>> &board) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. if (board.size() <= 1) return;
  7. int rows = board.size();
  8. int cols = board[0].size();
  9. vector<vector<bool>> visited(rows, vector<bool>(cols, false));
  10. vector<vector<bool>> checked(rows, vector<bool>(cols, false));
  11. for (int i = 0; i < rows; ++i) {
  12. for (int j = 0; j < cols; ++j) {
  13. if (!visited[i][j]) {
  14. if (board[i][j] == 'O') {
  15. if (checkIfSurrounded(i, j, board, visited, checked)) {
  16. markChecked(i, j, board, checked);
  17. }
  18. }
  19. else {
  20. visited[i][j] = true;
  21. }
  22. }
  23. }
  24. }
  25. }
  26. bool checkIfSurrounded(int i, int j, vector<vector<char>> &board,
  27. vector<vector<bool>> &visited, vector<vector<bool>> &checked) {
  28. queue<Pos> q;
  29. bool result = true;
  30. q.push(Pos(i, j));
  31. visited[i][j] = true;
  32. while (!q.empty()) {
  33. Pos pos = q.front();
  34. q.pop();
  35. i = pos.row;
  36. j = pos.col;
  37. checked[i][j] = true;
  38. if (j - 1 >= 0) {
  39. if (board[i][j - 1] == 'O' && !visited[i][j - 1]) {
  40. q.push(Pos(i, j - 1));
  41. visited[i][j - 1] = true;
  42. }
  43. }
  44. else {
  45. result = false;
  46. }
  47. if (j + 1 < board[0].size()) {
  48. if (board[i][j + 1] == 'O' && !visited[i][j + 1]) {
  49. q.push(Pos(i, j + 1));
  50. visited[i][j + 1] = true;
  51. }
  52. }
  53. else {
  54. result = false;
  55. }
  56. if (i + 1 < board.size()) {
  57. if (board[i + 1][j] == 'O' && !visited[i + 1][j]) {
  58. q.push(Pos(i + 1, j));
  59. visited[i + 1][j] = true;
  60. }
  61. }
  62. else {
  63. result = false;
  64. }
  65. if (i - 1 >= 0) {
  66. if (board[i - 1][j] == 'O' && !visited[i - 1][j]) {
  67. q.push(Pos(i - 1, j));
  68. visited[i - 1][j] = true;
  69. }
  70. }
  71. else {
  72. result = false;
  73. }
  74. }
  75.  
  76. return result;
  77. }
  78. void markChecked(int i, int j, vector<vector<char>> &board,
  79. vector<vector<bool>> &checked) {
  80. queue<Pos> q;
  81. bool result = true;
  82. q.push(Pos(i, j));
  83. while (!q.empty()) {
  84. Pos pos = q.front();
  85. q.pop();
  86. i = pos.row;
  87. j = pos.col;
  88. board[i][j] = 'X';
  89. checked[i][j] = false;
  90. if (j - 1 >= 0) {
  91. if (checked[i][j - 1]) {
  92. q.push(Pos(i, j - 1));
  93. checked[i][j - 1] = false;
  94. }
  95. }
  96. if (j + 1 < board[0].size()) {
  97. if (checked[i][j + 1]) {
  98. q.push(Pos(i, j + 1));
  99. checked[i][j + 1] = false;
  100. }
  101. }
  102. if (i + 1 < board.size()) {
  103. if (checked[i + 1][j]) {
  104. q.push(Pos(i + 1, j));
  105. checked[i + 1][j] = false;
  106. }
  107. }
  108. if (i - 1 >= 0) {
  109. if (checked[i - 1][j]) {
  110. q.push(Pos(i - 1, j));
  111. checked[i - 1][j] = false;
  112. }
  113. }
  114. }
  115. }
  116. typedef struct Pos {
  117. int row, col;
  118. Pos(int i, int j): row(i), col(j) {};
  119. };
  120. };

No comments:

Post a Comment