leetcode 130. Surrounded Regions 典型的深度优先遍历DFS + 矩阵遍历

- 日理万妓 2022-06-08 05:36 303阅读 0赞

Given a 2D board containing ‘X’ and ‘O’ (the letter 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

从矩形最外面一圈开始逐渐向里拓展。 若 O 是在矩形最外圈,它肯定不会被 X 包围,与它相连(邻)的 O 也就不可能被 X 包围,也就不会被替换,所以我们的工作主要是找出:
1)最外圈的 O
2)与最外圈的 O 相连的 O
3) 将上述找到的元素替换为某种标识符,代码中使用“#”
4.)最后按先行后列的顺序遍历矩形,找到没有被替换为 O 的元素,它们就是被 X 完全包围的需要被替换为 X 的元素;同时,标记为 # 的元素是没有被 X 包围的元素,此时将它们变回原来的 O

这道题的亮点是从问题的对立面来考虑做得,是很经典的DFS深度优先遍历的做法,很棒

代码如下:

  1. import java.util.Stack;
  2. class Point
  3. {
  4. int row;
  5. int col;
  6. Point(int a,int b)
  7. {
  8. row = a;
  9. col =b;
  10. }
  11. }
  12. /*
  13. * 这个用来学习BFS和DFS很有用
  14. *
  15. * 本题是要求把所有的内部的O替换为X,但是矩阵边界的O不算,
  16. * 本题是从反面来考虑的,把所有不能替换的O使用*来做一个标记
  17. *
  18. * 本题是通过stack来完成深度优先遍历的,使用BFS或者递归去做
  19. * 是同样的做法
  20. *
  21. * */
  22. public class Solution
  23. {
  24. public void solve(char[][] board)
  25. {
  26. if(board==null || board.length<=0)
  27. return;
  28. int row = board.length;
  29. int col = board[0].length;
  30. if(row<=2 || col<=2)
  31. return;
  32. boolean [][]visit = new boolean[row][col];
  33. for(int i=0;i<row;i++)
  34. {
  35. for(int j=0;j<col;j++)
  36. {
  37. if(board[i][j]=='O')
  38. {
  39. if(i==0||i==row-1 ||j==0||j==col-1)
  40. dfs(visit,board,i,j,row,col);
  41. }
  42. }
  43. }
  44. for(int i=0;i<row;i++)
  45. {
  46. for(int j=0;j<col;j++)
  47. {
  48. if(board[i][j]=='*')
  49. board[i][j]='O';
  50. else if(board[i][j]=='O')
  51. board[i][j]='X';
  52. }
  53. }
  54. }
  55. private void dfs(boolean [][]visit,char[][] board, int x, int y, int row, int col)
  56. {
  57. board[x][y]='*';
  58. Stack<Point> stack = new Stack<>();
  59. stack.push(new Point(x,y));
  60. visit[x][y]=true;
  61. while(stack.empty()==false)
  62. {
  63. Point top = stack.peek();
  64. stack.pop();
  65. if(top.row>0 && board[top.row-1][top.col]=='O' && visit[top.row-1][top.col]==false)
  66. {
  67. board[top.row-1][top.col]='*';
  68. visit[top.row-1][top.col]=true;
  69. stack.push(new Point(top.row-1, top.col));
  70. }
  71. if(top.row+1<row && board[top.row+1][top.col]=='O' && visit[top.row+1][top.col]==false)
  72. {
  73. board[top.row+1][top.col]='*';
  74. visit[top.row+1][top.col]=true;
  75. stack.push(new Point(top.row+1, top.col));
  76. }
  77. if(top.col>0 && board[top.row][top.col-1]=='O' && visit[top.row][top.col-1]==false)
  78. {
  79. board[top.row][top.col-1]='*';
  80. visit[top.row][top.col-1]=true;
  81. stack.push(new Point(top.row, top.col-1));
  82. }
  83. if(top.col+1<col && board[top.row][top.col+1]=='O' && visit[top.row][top.col+1]==false)
  84. {
  85. board[top.row][top.col+1]='*';
  86. visit[top.row][top.col+1]=true;
  87. stack.push(new Point(top.row, top.col+1));
  88. }
  89. }
  90. }
  91. }

下面是C++的做法,就是做一个DFS深度优先遍历,不过这道题我们是反其道而行之,通过寻找不符合条件的O,标记为*,然后把剩下的O填充为X即可完成本体的所有要求

代码如下:

  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <unordered_map>
  5. #include <set>
  6. #include <unordered_set>
  7. #include <queue>
  8. #include <stack>
  9. #include <string>
  10. #include <climits>
  11. #include <algorithm>
  12. #include <sstream>
  13. #include <functional>
  14. #include <bitset>
  15. #include <numeric>
  16. #include <cmath>
  17. #include <regex>
  18. #include <iomanip>
  19. #include <cstdlib>
  20. #include <ctime>
  21. using namespace std;
  22. class Solution
  23. {
  24. public:
  25. void solve(vector<vector<char>>& b)
  26. {
  27. if (b.size() <= 0)
  28. return;
  29. int row = b.size(), col = b[0].size();
  30. vector<vector<bool>> visit(row, vector<bool>(col, false));
  31. for (int i = 0; i < row; i++)
  32. {
  33. for (int j = 0; j < col; j++)
  34. {
  35. if (i == 0 || i == row - 1 || j == 0 || j == col - 1)
  36. dfs(b,visit, i, j);
  37. }
  38. }
  39. for (int i = 0; i < row; i++)
  40. {
  41. for (int j = 0; j < col; j++)
  42. {
  43. if (b[i][j] == 'O')
  44. b[i][j] = 'X';
  45. else if(b[i][j] == '*')
  46. b[i][j] = 'O';
  47. }
  48. }
  49. }
  50. void dfs(vector<vector<char>>& b, vector<vector<bool>>& visit, int x, int y)
  51. {
  52. int row = b.size(), col = b[0].size();
  53. if (x < 0 || x >= row || y < 0 || y >= col || visit[x][y] == true || b[x][y] != 'O')
  54. return;
  55. else
  56. {
  57. visit[x][y] = true;
  58. b[x][y] = '*';
  59. dfs(b, visit, x - 1, y);
  60. dfs(b, visit, x + 1, y);
  61. dfs(b, visit, x, y - 1);
  62. dfs(b, visit, x, y + 1);
  63. }
  64. }
  65. };

发表评论

表情:
评论列表 (有 0 条评论,303人围观)

还没有评论,来说两句吧...

相关阅读