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
解题思路:

对最外面一圈进行BFS,替换掉最外圈的连通范围,剩下的‘O’都是被包围的,就可以直接kill掉,JAVA实现如下:

	static public void solve(char[][] board) {
if (board.length <= 2 || board[0].length <= 2)
return;
for (int row = 0; row < board.length; row++) {
if (board[row][0] == 'O') {
board[row][0] = 'T';
bfs(board, row * board[0].length);
}
if (board[row][board[0].length - 1] == 'O') {
board[row][board[0].length - 1] = 'T';
bfs(board, row * board[0].length + board[0].length - 1);
}
}
for (int col = 1; col < board[0].length - 1; col++) {
if (board[0][col] == 'O') {
board[0][col] = 'T';
bfs(board, col);
}
if (board[board.length - 1][col] == 'O') {
board[board.length - 1][col] = 'T';
bfs(board, (board.length - 1) * board[0].length + col);
}
}
for (int row = 0; row < board.length; row++)
for (int col = 0; col < board[0].length; col++) {
if (board[row][col] == 'T')
board[row][col] = 'O';
else if (board[row][col] == 'O')
board[row][col] = 'X';
} } static public void bfs(char[][] board, int num) {
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(num);
while (!queue.isEmpty()) {
num=queue.poll();
int row = num / board[0].length;
int col = num - row * board[0].length;
if (row - 1 >= 0 && board[row - 1][col] == 'O') {
board[row - 1][col] = 'T';
queue.add(num - board[0].length);
}
if (row + 1 <= board.length - 1 && board[row + 1][col] == 'O') {
board[row + 1][col] = 'T';
queue.add(num + board[0].length);
}
if (col - 1 >= 0 && board[row][col - 1] == 'O') {
board[row][col - 1] = 'T';
queue.add(num - 1);
}
if (col + 1 <= board[0].length - 1 && board[row][col + 1] == 'O') {
board[row][col + 1] = 'T';
queue.add(num + 1);
}
}
}

Java for LeetCode 130 Surrounded Regions的更多相关文章

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

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

  2. Leetcode 130. Surrounded Regions

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

  3. leetcode 130 Surrounded Regions(BFS)

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

  4. Leetcode 130 Surrounded Regions DFS

    将内部的O点变成X input X X X XX O O X X X O XX O X X output X X X XX X X XX X X XX O X X DFS的基本框架是 void dfs ...

  5. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  6. 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...

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

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

  8. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

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

  9. 【leetcode】Surrounded Regions

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

随机推荐

  1. IOS7开发~UIKit Dynamics

    UIKit Dynamics的中文名称:有叫UIKit动力,也有叫UIKit动力模型和UIKit动态或者动态UI,叫什么名不要紧,理解就含义就可以了. 什么是UIKit Dynamics ? UIKi ...

  2. 【div+css】两个div,如何让内层的div在外层div中水平垂直居中

    好久没有写样式,很是很生疏 ==================================================================== 方法1: .parent { wi ...

  3. hdu1017(C++)

    这个题目很水,但是卡了格式和m=0的情况,wa了好多次,题目只给出N=1,感觉没说清楚 #include<iostream>using namespace std;int main(){ ...

  4. apache服务器日志及重启方法

    进入  lamp安装目录 ./ctlscript.sh restart 重启 实时查看日志 tail -f error_log 查看日志方法  404 及某天的方法cat access_log_201 ...

  5. 好工具MyEclise2016 CI下载

    地址:http://pan.baidu.com/s/1gfBw9Ab 安装后,点开crack目录,按步骤走. 下面是我安装成功的画面.

  6. js 查找一串字符串中一段字符

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. $ is not defined

    $ is not defined 引入Jquery的顺序不正确,要把它放在第一个引入

  8. MySQL具体解释(14)----------事务处理

    前言:前一篇文章关于事务处理的博文没有写清楚,读起来非常晦涩.非常难理解,所以有整理了一些资料,帮助理解.见谅! 关于MySQL事务处理学习记 START TRANSACTION COMMIT ROL ...

  9. bottle的几个小坑

    距离我在<web.py应用工具库:webpyext>里说要换用bottle,已经过去快两个月了--事实上在那之前我已经開始着手在换了.眼下那个用于 Backbone.js 介绍的样例程序已 ...

  10. MP4文件格式的解析,以及MP4文件的分割算法

    http://www.cnblogs.com/haibindev/archive/2011/10/17/2214518.html http://blog.csdn.net/pirateleo/arti ...