给定一个二维的矩阵,包含 ‘X’ 和 ‘O’(字母 O)。
找到所有被 ‘X’ 围绕的区域,并将这些区域里所有的 ‘O’ 用 ‘X’ 填充。
示例:
X X X X
X O O X
X X O X
X O X X
运行你的函数后,矩阵变为:
X X X X
X X X X
X X X X
X O X X
解释:
被围绕的区间不会存在于边界上,换句话说,任何边界上的 ‘O’ 都不会被填充为 ‘X’。 任何不在边界上,或不与边界上的 ‘O’ 相连的 ‘O’ 最终都会被填充为 ‘X’。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/surrounded-regions
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
Set<Integer> visited;
public void solve(char[][] board) {
if(board == null || board.length == 0) return;
visited = new HashSet<>();
int col = board[0].length;
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if((i == 0 || j == 0 || i == board.length - 1 || j == board[0].length - 1) && board[i][j] == 'O'){
dfs(board, i, j);
}
}
}
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(board[i][j] == 'O'){
board[i][j] = 'X';
} else if(board[i][j] == '#'){
board[i][j] = 'O';
}
}
}
}
public void dfs(char[][] board, int i, int j) {
if(i < 0 || j < 0 || i == board.length || j == board[0].length) return;
if(board[i][j] == '#' || board[i][j] == 'X') return;
board[i][j] = '#';
dfs(board, i-1, j);
dfs(board, i+1, j);
dfs(board, i, j+1);
dfs(board, i, j-1);
}
}
class Solution {
Set<Integer> visited;
public void solve(char[][] board) {
if(board == null || board.length == 0) return;
visited = new HashSet<>();
Queue<Integer> queue = new LinkedList<>();
int col = board[0].length, row = board.length;
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if((i == 0 || j == 0 || i == board.length - 1 || j == board[0].length - 1) && board[i][j] == 'O'){
queue.offer(i * col + j);
while(!queue.isEmpty()){
int sz = queue.size();
while(sz-- > 0){
int sum = queue.poll();
int nr = sum / col, nc = sum % col;
board[nr][nc] = '#';
if(nr > 0 && board[nr-1][nc] == 'O') queue.offer((nr-1) * col + nc);
if(nr < row - 1 && board[nr+1][nc] == 'O') queue.offer((nr+1) * col + nc);
if(nc > 0 && board[nr][nc-1] == 'O') queue.offer(nr * col + nc - 1);
if(nc < col - 1 && board[nr][nc+1] == 'O') queue.offer(nr * col + nc + 1);
}
}
}
}
}
for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(board[i][j] == 'O'){
board[i][j] = 'X';
} else if(board[i][j] == '#'){
board[i][j] = 'O';
}
}
}
}
}
并查集
class Solution {
class UF{
private int[] parent;
private int[] size;
private int count;
public UF(int n){
parent = new int[n];
size = new int[n];
count = n;
for(int i = 0; i < n; i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int x){
while(parent[x] != x){
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
public void union(int x, int y){
int par1 = find(x);
int par2 = find(y);
if(par2 == par1) return;
count--;
if(size[par1] > size[par2]){
parent[par2] = par1;
size[par2] += size[par1];
} else {
parent[par1] = par2;
size[par1] += size[par2];
}
}
public boolean isConnected(int x, int y){
int par1 = find(x);
int par2 = find(y);
return par2 == par1;
}
}
public void solve(char[][] board) {
if(board == null || board.length == 0) return;
int col = board[0].length, row = board.length;
UF uf = new UF(col * row + 1);
int dummy = col*row;
for(int i = 0; i < row; i++){
if(board[i][0] == 'O') uf.union(i*col, dummy);
if(board[i][col-1] == 'O') uf.union(i * col + col - 1, dummy);
}
for(int i = 0; i < col; i++){
if(board[0][i] == 'O') uf.union(i, dummy);
if(board[row-1][i] == 'O') uf.union((row-1)*col+i, dummy);
}
int[][] d = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};
for(int i = 1; i < row - 1; i++){
for(int j = 1; j < col - 1; j++){
if(board[i][j] == 'O'){
for(int k = 0; k < 4; k++){
int x = i + d[k][0];
int y = j + d[k][1];
if(board[x][y] == 'O') uf.union(x*col+y, i*col+j);
}
}
}
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(board[i][j] == 'O' && !uf.isConnected(i * col + j, dummy)){
board[i][j] = 'X';
}
}
}
}
}