/*
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
A region i...
![subject](/tpl/images/cats/informatica.png)
Computers and Technology, 06.07.2021 17:10 NoireDemon
/*
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
*/
public class Solution {
// This algorithm cannot solve the large test set
public void solve(char[][] board) {
// Start typing your Java solution below
// DO NOT write main() function
int rows = board. length;
if(rows == 0) return;
int cols = board[0].length;
for(int i = 0; i < cols; i++) {
// check first row's O
if(board[0][i] == 'O') {
// change it to other symbol
board[0][i] = '#';
dfs(board, 0, i);
}
// check the last row
if(board[rows - 1][i] == 'O') {
board[rows - 1][i] = '#';
dfs(board, rows - 1, i);
}
}
for(int i = 0; i < rows; i++) {
// check first col
if(board[i][0] == 'O') {
board[i][0] = '#';
dfs(board, i, 0);
}
// check last col
if(board[i][cols - 1] == 'O') {
board[i][cols - 1] = '#';
dfs(board, i, cols - 1);
}
}
// change O to X
changeTo(board, 'O', 'X');
// change # to O
changeTo(board, '#', 'O');
return;
}
public void dfs(char[][] board, int row, int col) {
// check up
if(row > 0) {
if(board[row - 1][col] == 'O') {
board[row - 1][col] = '#';
dfs(board, row - 1, col);
}
}
// check left
if(col > 0) {
if(board[row][col - 1] == 'O') {
board[row][col - 1] = '#';
dfs(board, row, col - 1);
}
}
// check right
if(row < board. length - 1) {
if(board[row + 1][col] == 'O') {
board[row+1][col] = '#';
dfs(board, row+1, col);
}
}
// check down
if(col < board[0].length - 1) {
if(board[row][col+1] == 'O'){
board[row][col+1] = '#';
dfs(board, row, col+1);
}
}
return;
}
public void changeTo(char[][] board, char from, char to) {
for(int i = 0; i < board. length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(board[i][j] == from) {
board[i][j] = to;
}
}
}
return;
}
}
![ansver](/tpl/images/cats/User.png)
Answers: 2
![](/tpl/images/ask_question.png)
![](/tpl/images/ask_question_mob.png)
Another question on Computers and Technology
![question](/tpl/images/cats/informatica.png)
Computers and Technology, 23.06.2019 11:30
Me dangers of social media and the internetexplain what each means: 1) social media and phones have become an addiction.2) outside people have access to you all the time.3) cyberstalking4) cyberbullying5) catphishing6) viruses7) identity theft8) credit card fraud9) hacking10) money schemes
Answers: 1
![question](/tpl/images/cats/informatica.png)
Computers and Technology, 23.06.2019 20:30
What is the biggest difference between section breaks and regular page breaks
Answers: 1
![question](/tpl/images/cats/informatica.png)
Computers and Technology, 24.06.2019 03:00
Click the "draw structure" button to activate the drawing utility. draw two diastereomers of (1z,4r)−1,4−dimethylcyclodecene and name them, including (e)/(z) and (r)/(s) notation. part 1 out of 4 draw the diastereomer containing a chiral center with s configuration here. window open
Answers: 1
![question](/tpl/images/cats/informatica.png)
Computers and Technology, 24.06.2019 07:30
John recently worked on a project about various programming languages. he learned that though procedural language programs are useful, they have disadvantages too. what is a disadvantage of programs written in procedural languages? a. programs do not represent data complexity. b. programs take more time to execute. c. programs are prone to security threats. d. programs do not interface with multiple platforms.
Answers: 3
You know the right answer?
Questions
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/health.png)
Health, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/biologiya.png)
Biology, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/en.png)
English, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/obshestvoznanie.png)
Social Studies, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/mat.png)
Mathematics, 14.09.2020 15:01
![question](/tpl/images/cats/istoriya.png)
History, 14.09.2020 15:01