问题描述
给定一个二维的矩阵,包含 '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
解答
/* 本题给定的矩阵中有三种元素: 字母 X; 被字母 X 包围的字母 O; 没有被字母 X 包围的字母 O。 本题要求将所有被字母 X 包围的字母 O都变为字母 X ,但很难判断哪些 O 是被包围的,哪些 O 不是被包围的。 注意到题目解释中提到:任何边界上的 O 都不会被填充为 X。 我们可以想到,所有的不被包围的 O 都直接或间接与边界上的 O 相连。我们可以利用这个性质判断 O 是否在边界上,具体地说: 对于每一个边界上的 O,我们以它为起点,标记所有与它直接或间接相连的字母 O; 最后我们遍历这个矩阵,对于每一个字母: 如果该字母被标记过,则该字母为没有被字母 X 包围的字母 O,我们将其还原为字母 O; 如果该字母没有被标记过,则该字母为被字母 X 包围的字母 O,我们将其修改为字母 X。 */ class Solution { boolean[][] visited; public void mark(int row, int column, int x, int y, char board[][]){ if(x<0 || x==row || y<0 || y==column || board[x][y]!='O' || visited[x][y])return; board[x][y] = 'M'; visited[x][y] = true; mark(row, column, x+1, y, board); mark(row, column, x-1, y, board); mark(row, column, x, y+1, board); mark(row, column, x, y-1, board); visited[x][y] = false; } public void solve(char[][] board) { int row = board.length; if(row <= 1)return; int column = board[0].length; if(column <= 1)return; visited = new boolean[row][column]; for(int i=0;i<row;i++){//mark第一列 if(board[i][0] == 'O')mark(row, column, i, 0, board); } for(int j=1;j<column;j++){//mark第一行 if(board[0][j] == 'O')mark(row, column, 0, j, board); } for(int j=1;j<column;j++){//mark最后一行 if(board[row-1][j] == 'O')mark(row, column, row-1, j, board); } for(int i=0;i<row-1;i++){//mark最后一列 if(board[i][column-1] == 'O')mark(row, column, i, column-1, board); } for(int i=0;i<row;i++){ for(int j=0;j<column;j++){ if(board[i][j] == 'O')board[i][j] = 'X'; if(board[i][j] == 'M')board[i][j] = 'O'; } } } } /*保存坐标,但是太低效 class Solution { Map<String, Integer> visited; Map<String, Integer> invailed; public int flood(int row, int column, int x, int y, char[][] board){ if(board[x][y] == 'X' || visited.containsKey(x+","+y) || invailed.containsKey(x+","+y))return 0; visited.put(x+","+y, 1); //System.out.println(x+"-->"+y+"comes"); if(x == 0 || x == row-1 || y == 0 || y == column-1){ visited.remove(x+","+y); if(board[x][y] == 'O')return -1; else return 0; }else{ board[x][y] = 'X'; int sum = 0; sum += flood(row, column, x+1, y, board); sum += flood(row, column, x-1, y, board); sum += flood(row, column, x, y+1, board); sum += flood(row, column, x, y-1, board); //System.out.println(x+","+y+"-->"+sum); return sum; } } public void solve(char[][] board) { int row = board.length; if(row <= 1)return; int column = board[0].length; if(column <= 1)return; visited = new HashMap<String, Integer>(); invailed = new HashMap<String, Integer>(); for(int i=1;i<row-1;i++){ for(int j=1;j<column-1;j++){ if(board[i][j] == 'X' || invailed.containsKey(i+","+j))continue; int sum = flood(row, column, i, j, board); //System.out.println(i+" "+j+"-->"+sum); if(sum < 0){ for(Map.Entry<String, Integer> entry:visited.entrySet()){ String[] coordination = entry.getKey().split(","); board[Integer.parseInt(coordination[0])][Integer.parseInt(coordination[1])] = 'O'; //System.out.println(x+"-->"+y+board[x][y]+"origin:"+entry.getKey()); } invailed.putAll(visited); } visited.clear(); } } invailed.clear(); } } */ /*超时。。。 class Solution { boolean[][] visited; public int flood(int row, int column, int x, int y, char[][] board){ if(board[x][y] == 'X' || visited[x][y])return 0; visited[x][y] = true; if(x == 0 || x == row-1 || y == 0 || y == column-1){ visited[x][y] = false; if(board[x][y] == 'O')return -1; else return 0; }else{ int sum = 0; sum += flood(row, column, x+1, y, board); sum += flood(row, column, x-1, y, board); sum += flood(row, column, x, y+1, board); sum += flood(row, column, x, y-1, board); visited[x][y] = false; //System.out.println(x+","+y+" "+sum+"-->"+visited[x][y]); return sum; } } public void solve(char[][] board) { int row = board.length; if(row <= 1)return; int column = board[0].length; if(column <= 1)return; visited = new boolean[row][column]; for(int i=1;i<row-1;i++){ for(int j=1;j<column-1;j++){ if(board[i][j] == 'X' || visited[i][j])continue; int sum = flood(row, column, i, j, board); if(sum == 0)board[i][j] = 'X'; } } } } */