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 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
这道题要考虑的不是去寻找哪些被X包围,应该考虑从四个边界,找出与O相连的就是没有被包围的。这里用的是BFS从四个边界进行BFS找出与边界O相连的做好标记,这里是将其变成Z。
BFS完成以后这个board 进行遍历,如果是Z则变成O,其余的都变成X
BFS使用一个队列,将其周围满足条件的都加入到队列中,这里不用使用递归
 import java.util.LinkedList;
import java.util.Queue; public class Solution {
private char board[][];
private int rows;
private int cols; //行和列数
private Queue<Integer> queue = new LinkedList<Integer>(); //BFS使用的队列 public void solve(char[][] board) {
this.board = board;
if(null == board || board.length == 0 || board[0].length == 0) //特殊的直接返回
return;
rows = this.board.length;
cols = this.board[0].length; //获取行和列的数目 for(int i = 0; i < rows; i++){
traver(i, 0); //第一列
traver(i, cols - 1); //最后一列
}
for(int i = 0; i < cols; i++){
traver(0, i); //第一行
traver(rows - 1, i); //最后一行
}
//遍历整个数组
for(int i = 0; i < rows;i++){
for(int j = 0; j < cols; j++){
board[i][j] = this.board[i][j] == 'Z' ? 'O' : 'X';
}
}
} /**
* 对x,y所指的单元进行BFS
* @param x
* @param y
*/
public void traver(int x, int y){
add(x, y);
while(!this.queue.isEmpty()){
int head = queue.poll(); //出队列
int temp_x = head / cols;
int temp_y = head % cols;
add(temp_x - 1, temp_y);
add(temp_x + 1, temp_y);
add(temp_x, temp_y - 1);
add(temp_x, temp_y + 1); //flood fill算法的体现
}
} /**
* x,y所指的单元如果是'o'放到队列中,x * cols + y
* @param x
* @param y
*/
public void add(int x, int y){
if(x >= 0 && x < rows && y >= 0 && y < cols && this.board[x][y] == 'O')
{
this.queue.add(x * cols + y);
this.board[x][y] = 'Z';
}
} }

BFS, FLOOD FILL...待续

参考:http://blog.csdn.net/pickless/article/details/12074363

洪泛算法参考:http://blog.sina.com.cn/s/blog_7506816f0100pqzn.html

http://blog.csdn.net/jia20003/article/details/8908464

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. [LintCode] Surrounded Regions 包围区域

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

  5. 22. Surrounded Regions

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

  6. [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions

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

  7. 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 用了第一种方式, ...

  8. 130. Surrounded Regions(M)

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

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

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

随机推荐

  1. 触发器修改后保存之前的数据 表中插入数据时ID自动增长

    create or replace trigger t before update on test5 for each rowbegin insert into test55 values (:old ...

  2. Windows装机指南

    开发相关: Anaconda整合了很多python的dependency,方便使用

  3. JQuery.Gantt(甘特图)开发

    一.简介 JQuery.Gantt是一个开源的基于JQuery库的用于实现甘特图效果的可扩展功能的JS组件库. 二.前端页面 2.1 资源引用 首先需要将下载到的源码中的CSS.IMG.JS等资源放入 ...

  4. Backbone.js学习之初识hello-world

    说了好久好久要学习Backbone.js,现在终于下定决心开始学习了.然后呢,就根据我的学习进度在这里做个简单的记录,方便新人,也方便我自己以后回忆. 准备 用bower下载这几个库或框架也是醉了.. ...

  5. 【CSS3】---only-child选择器+only-of-type选择器

    only-child选择器 “:only-child”选择器选择的是父元素中只有一个子元素,而且只有唯一的一个子元素.也就是说,匹配的元素的父元素中仅有一个子元素,而且是一个唯一的子元素. 示例演示 ...

  6. Android 开源项目分类汇总

    Android 开源项目分类汇总 Android 开源项目第一篇——个性化控件(View)篇  包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView ...

  7. js正则表达式的验证示例

    //验证邮箱的JS正则 <script type="text/javascript"> $(function() { $("#inputemail" ...

  8. Movie importing requires quicktime

    在Unity中使用MovieTexture播放视频会碰到movie importing requires quicktime的错误,解决方法如下: 1.关闭Unity安装QuickTime播放器,打开 ...

  9. libjpeg 交叉编译动态库和静态库

    1.下载libjpeg库,解压之     得到了jpeg6b和libtool-2.2.4两个文件夹. 2.编译安装libtool工具.   这是配置libtool,这里需要注意:configure 参 ...

  10. (转)一网打尽当下NoSQL类型、适用场景及使用公司

    摘要:对比传统关系型数据库,NoSQL有着更为复杂的分类——键值.面向文档.列存储以及图数据库.这里就带你一览NoSQL各种类型的适用场景及一些知名公司的方案选择. 在过去几年,关系型数据库一直是数据 ...