subject

/* 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
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 19:40
Write the quartstogallons java class that declares a named constant to hold the number of quarts in a gallon (4). also declare a variable to represent the number of quarts needed for a painting job, and assign an appropriate value—for example, 18. compute and display the number of gallons and quarts needed for the job. display explanatory text with the values, for example:
Answers: 2
question
Computers and Technology, 21.06.2019 19:40
Microsoft introduced active directory with windows 2000. in server 2012, it is in now version 4. what are some new features in the new ad that were not present in windows 2000 ad?
Answers: 1
question
Computers and Technology, 22.06.2019 12:10
Linux is distributed under gnu gpl. why is this important? a. it ensures that only torvalds can profit from the sale of linux b. it prevents unknowledgeable users from downloading programs they don't know how to operate. c. it provides protection for the developers who created linux. d. it states that anyone can copy, modify, and share the program if changes are made public.
Answers: 1
question
Computers and Technology, 24.06.2019 00:20
Describe a data structures that supports the stack push and pop operations and a third operation findmin, which returns the smallest element in the data structure, all in o(1) worst-case time.
Answers: 2
You know the right answer?
/* Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
A region i...
Questions
question
Chemistry, 31.01.2020 01:50
question
Physics, 31.01.2020 01:50
Questions on the website: 13722361