当前位置:网站首页>Surrounded area
Surrounded area
2022-07-27 01:19:00 【Coffee without ice or sugar】
Title Description :
Given a two-dimensional matrix , contain ‘X’ and ‘O’( Letter O).
Find all the victims ‘X’ The surrounding area , And put all of these areas ‘O’ use ‘X’ fill .
Thought analysis :
This question is particularly similar to the number of islands , The difficulty of this problem lies in The boundary of the 0 And internal multiple 0 Handling of the connectivity of , Here's the picture :

Bottom boundary 0 And internal 0 It's connected , So there is no need to put these 0 Change it to X.
(1) We can make the boundary dfs Traverse , Connect all connected 0 find , Change to letter ‘A’;
(2) Traversal array , Element encountered ’0’, Change it to ’X’, Element encountered ‘A’, Change it to ’0’
You know ‘A’ There is no need to modify , So change it back .
Reference code :
public void solve(char[][] board) {
if(board == null || board.length == 0)return;
int m = board.length;
int n = board[0].length;
// All the left and right boundaries 0 The connectivity of is changed to 'A'
for(int i = 0; i < m; i++){
dfs(board, i, 0);
dfs(board, i, n - 1);
}
// All of the upper and lower boundaries 0 The connectivity of is changed to 'A'
for(int i = 1; i < n - 1; i++){
// Note here , When calculating above, put i=0 and i=n-1 It's all calculated
dfs(board, 0, i);
dfs(board, m - 1, i);
}
// take ‘A’ Change back ‘O’, To be changed ‘O’ Change it to ‘X’
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == 'A')board[i][j] = 'O';
else if(board[i][j] == 'O')board[i][j] = 'X';
}
}
}
//dfs Find all 0 Connected element , Change it to ‘A’
public void dfs(char[][] board, int i, int j){
if(i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != 'O')return;// Note here : Can not write board[i][j] == 'X'
if(board[i][j] == 'O')board[i][j] = 'A';// Key code
dfs(board, i + 1, j);
dfs(board, i - 1, j);
dfs(board, i, j + 1);
dfs(board, i, j - 1);
}Summary :
dfs In the judgment condition :board[i][j] != 'O’ It can't be changed to board[i][j] == ‘X’, If it is changed to the latter , Then for the following cases , Will all ‘O’ Change it to ‘A’ after ,board[i][j] == 'X’ To return to , because ‘A’ != ‘X’, So recursion will continue :

Similar topics :
边栏推荐
- 初中高三部曲音视频下载Pronunciation Pairs+Ship or Sheep+Tree or Three
- 吴恩达深度学习系列教学视频学习笔记(一)——用于二分类的logistic回归函数
- MTCNN
- Redis -- cache avalanche, cache penetration, cache breakdown
- SQL relational algebra - Division
- Calls to onsaveinstancestate and onrestoreinstancestate methods
- Simple explanation of database table connection
- markdown
- 07 - setup and attack of log server
- 什么是数字经济,它是如何改变商业模式的?
猜你喜欢

非递归前中后序遍历二叉树

5. Xshell connection server denied access, password error

Are you ready for the Internet of things to revolutionize manufacturing?

Jenkins--基础--5.2--系统配置--系统配置

Understanding of Flink checkpoint source code

李宏毅机器学习(2017版)_P5:误差

Li Hongyi machine learning (2021 Edition)_ P5-6: small gradient processing

4. Root user login

MTCNN

Simple explanation of database table connection
随机推荐
深度学习汇报(2)
Calls to onsaveinstancestate and onrestoreinstancestate methods
4. European Champions League
4.root用户登录
随着5G越来越普及,我们的生活会受到哪些积极的影响
c语言实现动态顺序表的增删查改
5. Legal bracket string
adb. Exe stopped working popup problem
李宏毅机器学习(2017版)_P21:卷积神经网络CNN
Come and help you understand the Internet of things in three minutes
What is kubernetes?
Tencent upgrades the live broadcast function of video Number applet. Tencent's foundation for continuous promotion of live broadcast is this technology called visual cube (mlvb)
5.xshell连接服务器拒绝访问,密码错误
How does KS catch salt value? api,did?
MySQL字符集设置为UTF-8,但控制台仍然出现中文乱码问题
Jenkins--基础--04--安装中文插件
Flink sliding window understanding & introduction to specific business scenarios
3. 拳王阿里
Verilog过程赋值语句
7. F1方程式冠军