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 18:00
Jenna updated her status on a social network regarding her upcoming vacation. what information should jenna not provide when updating her status on a social networking site? a. her favorite quote b. her likes in popular culture c. her routine d. her opinions on social issues res
Answers: 1
question
Computers and Technology, 22.06.2019 23:50
You need to design a circuit that implements the functions in the following table: s0 s1 function0 0 a + 10 1 a – b1 0 a + b1 1 a – 1s0 and s1 are 1-bit control inputs to select the function of the circuit. inputs a and b are 4-bitnumbers in 2s complement form. the output is also a 4-bit number in 2s complement form.you are allowed to use only one ttl 7483 4-bit adder to implement all the functions. but anynumber of other components (except the adder) can be used.hint: design a combinational logic circuit to modify the input b and the “carry input” of theadder depending on the control inputs s0 and s1.important: lab grade will depend on the working of the circuit & will be checked of by your labinstructor.1. is the output valid for the following input combinations: a. s0 = 0, s1 = 0, a = 7, b = 3? b. s0 = 0, s1 = 1, a = 7, b = 3? c. s0 = 1, s1 = 0, a = -4, b = -5? d. s0 = 1, s1 = 1, a = -8, b = 6? 2. what is the range of inputs (for both a and b) that will produce the valid output for all the functions?
Answers: 3
question
Computers and Technology, 23.06.2019 00:50
Representa os dados de um banco de dados como uma coleç? o de tabelas constituídas por um conjunto de atributos, que definem as propriedades ou características relevantes da entidade que representam. marque a alternativa que representa o modelo descrito no enunciado. escolha uma:
Answers: 3
question
Computers and Technology, 23.06.2019 10:00
Hey i just logged on and one of the moderators deleted a bunch of my answers to questions, even though the answers were right and the people it doesn't make sense but if anyone wants to talk about anything just message me lol (this is super random lol)
Answers: 1
You know the right answer?
/* Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.
A region i...
Questions
question
Mathematics, 11.05.2021 22:40
question
English, 11.05.2021 22:40
question
Chemistry, 11.05.2021 22:40
question
Arts, 11.05.2021 22:40
question
Mathematics, 11.05.2021 22:40
question
Arts, 11.05.2021 22:40
Questions on the website: 13722362