目录
一、题目描述
给定一个二维的矩阵,包含 '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'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。
二、解题思路
思路还是比较直接的,这类题在图和并查集当中属于一类很经典的题目。本质是求连通分量的个数。
通常这类题都有三种解法:
- 深度优先搜索
- 广度优先搜索
- 并查集
深度或广度搜索进行的次数即为连通块的数量;并查集则为最后有几个集合则有多少个连通块。
对于leetCode200来说,上面所说的做法就可以解决了。但是这一题不同的地方在于在边界上的'O'连通块是不用处理的,所以只需要把处于边界的连通块遍历出来即可。除了边界的连通块,其余地方的'O'全部改为'X'。
三、代码实现
内含转载大佬代码:
作者:liweiwei1419
链接:https://leetcode-cn.com/problems/surrounded-regions/solution/dfs-bing-cha-ji-java-by-liweiwei1419/
//本人写的dfs
#include<bits/stdc++.h>
using namespace std;
//遍历的四个方向
vector<int> dx = { 0,0,1,-1 };
vector<int> dy = { 1,-1,0,0 };
void dfs(int m, int n, vector<vector<bool>>& visited, vector<vector<char>>& board) {
visited[m][n] = true;
for (int i = 0; i < 4; i++) {
int x = m + dx[i];
int y = n + dy[i];
if (x >= 0 && x < board.size() && y >= 0 && y < board[0].size()) {
if (board[x][y] == 'O' && !visited[x][y]) {
dfs(x, y, visited, board);
}
}
}
}
void solve(vector<vector<char>>& board) {
int m = board.size();
if (m == 0) {
return;
}
int n = board[0].size();
vector < vector <bool>> visited(m, vector<bool>(n, false));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
//在最外围的位置
if (i == 0 || (i != 0 && (j == 0 || j == n - 1)) || i == m - 1) {
if (board[i][j] == 'O' && !visited[i][j]) {
dfs(i, j, visited, board);
}
}
}
}
//把所有没有被访问且为'O'的位置改掉
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (!visited[i][j] && board[i][j] == 'O') {
board[i][j] = 'X';
}
}
}
}
int main() {
vector<vector<char>> board = { {'X','X','X','X','X'},
{'X','O','O','O','X'},
{'X','X','X','O','X'},
{'X','X','O','X','X'},
{'X','X','O','X','X'} };
solve(board);
for (auto x : board) {
cout << x[0] << " " << x[1] << " " << x[2] << " " << x[3] << " " << x[4] << endl;
}
return 0;
}
//题解区大佬写的bfs和并查集(java),作者:liweiwei1419
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
public void solve(char[][] board) {
int rows = board.length;
if (rows == 0) {
return;
}
int cols = board[0].length;
int[][] directions = new int[][]{{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
// 第 1 步:把四周的 'O' 全部推入队列,通过广度优先遍历,把与 'O' 连通的地方全部编辑
Queue<int[]> queue = new LinkedList<>();
for (int i = 0; i < rows; i++) {
if (board[i][0] == 'O') {
queue.offer(new int[]{i, 0});
}
if (board[i][cols - 1] == 'O') {
queue.offer(new int[]{i, cols - 1});
}
}
for (int j = 1; j < cols - 1; j++) {
if (board[0][j] == 'O') {
queue.offer(new int[]{0, j});
}
if (board[rows - 1][j] == 'O') {
queue.offer(new int[]{rows - 1, j});
}
}
while (!queue.isEmpty()) {
int[] top = queue.poll();
int i = top[0];
int j = top[1];
board[i][j] = '-';
for (int[] direction : directions) {
int newX = i + direction[0];
int newY = j + direction[1];
if (inArea(newX, newY, rows, cols) && board[newX][newY] == 'O') {
queue.offer(new int[]{newX, newY});
}
}
}
// 第 2 步:恢复
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == '-') {
board[i][j] = 'O';
} else if (board[i][j] == 'O') {
board[i][j] = 'X';
}
}
}
}
private boolean inArea(int x, int y, int rows, int cols) {
return x >= 0 && x < rows && y >= 0 && y < cols;
}
}
//并查集
public class Solution {
public void solve(char[][] board) {
int rows = board.length;
if (rows == 0) {
return;
}
int cols = board[0].length;
if (cols == 0) {
return;
}
UnionFind unionFind = new UnionFind(rows * cols + 1);
int dummyNode = rows * cols;
// 填写第 1 行和最后一行
for (int j = 0; j < cols; j++) {
if (board[0][j] == 'O') {
unionFind.union(getIndex(0, j, cols), dummyNode);
}
if (board[rows - 1][j] == 'O') {
unionFind.union(getIndex(rows - 1, j, cols), dummyNode);
}
}
// 填写第 1 列和最后一列
for (int i = 1; i < rows - 1; i++) {
if (board[i][0] == 'O') {
unionFind.union(getIndex(i, 0, cols), dummyNode);
}
if (board[i][cols - 1] == 'O') {
unionFind.union(getIndex(i, cols - 1, cols), dummyNode);
}
}
int[][] directions = new int[][]{{0, 1}, {1, 0}};
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == 'O') {
for (int[] direction : directions) {
int newX = i + direction[0];
int newY = j + direction[1];
if (newX < rows && newY < cols && board[newX][newY] == 'O') {
unionFind.union(getIndex(i, j, cols), getIndex(newX, newY, cols));
}
}
}
}
}
for (int i = 1; i < rows - 1; i++) {
for (int j = 0; j < cols - 1; j++) {
if (board[i][j] == 'O') {
if (!unionFind.isConnected(getIndex(i, j, cols), dummyNode)) {
board[i][j] = 'X';
}
}
}
}
}
private int getIndex(int x, int y, int cols) {
return x * cols + y;
}
class UnionFind {
private int[] parent;
public UnionFind(int n) {
this.parent = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
public boolean isConnected(int x, int y) {
return find(x) == find(y);
}
public int find(int x) {
while (x != parent[x]) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
public void union(int x, int y) {
int xRoot = find(x);
int yRoot = find(y);
if (xRoot == yRoot) {
return;
}
parent[xRoot] = yRoot;
}
}
}