import java.util.Arrays;
import java.util.Stack; /**
* Source : https://oj.leetcode.com/problems/surrounded-regions/
*
*
* 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 O O X
* X X X X
* X O X X
* X X O 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 SurroundedRegions { /**
* 将所有被X包围的O替换为X
* 先找出不被包围的O替换为Y
* 然后将所有剩下的所有的O替换为X
* 最后将所有的Y替换为O
*
* 其中关键是将不被包围的O替换为Y,也就是要找出所有不被包围的O
* 从边缘开始,如果边缘处的元素为O则寻找其周围是否有为O的元素,直到没有O或者没有元素
*
*
* @param board
* @return
*/
public char[][] solve (char[][] board) {
if (board.length == 0) {
return board;
}
fillBoard(board, 'O', 'Y');
replace(board, 'O', 'X');
fillBoard(board, 'Y', 'O');
return board;
} /**
* 将board中的指定元素替换为另外一个
*
* @param board
* @param source
* @param target
*/
private void fillBoard (char[][] board, char source, char target) {
for (int i = 0; i < board.length; i++) {
fill(board, i, 0, source, target);
fill(board, i, board[0].length-1, source, target);
}
for (int i = 0; i < board[0].length; i++) {
fill(board, 0, i, source, target);
fill(board, board.length-1, i, source, target);
}
} private void fill (char[][] board, int i, int j, char source, char target) {
if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || board[i][j] != source) {
return;
}
Stack<Position> stack = new Stack<Position>();
stack.push(new Position(i, j)); while (stack.size() > 0) {
Position position = stack.pop();
board[position.i][position.j] = target;
if (position.i > 0 && board[position.i-1][position.j] == source) {
stack.push(new Position(position.i-1, position.j));
}
if (position.i < board.length-1 && board[position.i+1][position.j] == source) {
stack.push(new Position(position.i+1, position.j));
}
if (position.j > 0 && board[position.i][position.j-1] == source) {
stack.push(new Position(position.i, position.j-1));
}
if (position.j < board[0].length-1 && board[position.i][position.j+1] == source) {
stack.push(new Position(position.i, position.j+1));
}
}
} /**
* 将source替换为target
*
* @param board
* @param source
* @param target
*/
private void replace (char[][] board, char source, char target) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == source) {
board[i][j] = target;
}
}
}
} private static void print (char[][] board) {
for (int i = 0; i < board.length; i++) {
System.out.println(Arrays.toString(board[i]));
}
System.out.println();
} private class Position {
int i;
int j; public Position(int i, int j) {
this.i = i;
this.j = j;
}
} public static void main(String[] args) {
SurroundedRegions surroundedRegions = new SurroundedRegions();
char[][] board = {
{'X', 'X', 'X', 'X'},
{'X', 'X', 'X', 'X'},
{'X', 'O', 'X', 'X'},
{'X', 'X', 'O', 'X'}
};
print(surroundedRegions.solve(board));
} }

leetcode — surrounded-regions的更多相关文章

  1. [LeetCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  2. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  3. LeetCode: Surrounded Regions 解题报告

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  4. [leetcode]Surrounded Regions @ Python

    原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...

  5. Leetcode: Surrounded regions

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  6. LEETCODE —— Surrounded Regions

    Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...

  7. LeetCode: Surrounded Regions [130]

    [题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...

  8. [LeetCode] Surrounded Regions 广度搜索

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  9. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  10. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

随机推荐

  1. centos7 安装python3.7.11 笔记

    安装python依赖包yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-deve ...

  2. Notepad++常用快捷键

    Ctrl-H    打开Find / Replace 对话框 Ctrl-D    复制当前行 Ctrl-L    删除当前行 Ctrl-T    上下行交换 F3    找下一个 Shift-F3   ...

  3. laravel 目录权限

    chown -R www:www /data/wwwroot   #变更目录所有者并向下传递 find /data/wwwroot/ -type d -exec chmod 755 {} \;   # ...

  4. MS SQL计算最大公约数和最小公倍数函数

    /*求两个数的最大公约数*/ CREATE FUNCTION f_GetGys ( @num1 BIGINT , @num2 BIGINT ) RETURNS BIGINT AS BEGIN DECL ...

  5. 像屎一样的 Spring Boot入门,总算有反应了

    我特么最烦的就是现在Java不知道抽什么风,喜欢用maven这种,怎么搞都会有错误提示的玩意.搞个spring boot,官方的所谓http://start.spring.io/生成的项目启动不了. ...

  6. 如何用VSCode调试Vue.js

    VS Code相关插件:Chinese (Simplified) Language Pack for Visual Studio Code Debugger for Chrome ESLint Vet ...

  7. data自定义属性获取方法和设置

    <!--原生获取方法--> <div data-id="id=1"></div> <script> //js原生获取方法 var i ...

  8. [Swift]LeetCode458. 可怜的小猪 | Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...

  9. [Swift]LeetCode649. Dota2 参议院 | Dota2 Senate

    In the world of Dota2, there are two parties: the Radiantand the Dire. The Dota2 senate consists of ...

  10. [Swift]LeetCode353. 设计贪吃蛇游戏 $ Design Snake Game

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...