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. Linux 网卡设备驱动程序设计(3)

    三.网络子系统深度分析 用户程序通过网络发送这个网络数据包 通过 SCI 协议无关接口 协议栈 <   UDP的实现  会选择路由 <    IP的实现  会建立这个邻居子系统,建立邻居信 ...

  2. Android studio 安装和使用

    之前一直是使用eclipse+ADT+SDK进行Android的开发的,不愿意转到Android studio是因为熟悉了eclipse的开发环境,最近偶然使用了android studio,感觉比使 ...

  3. opensuse pptp拨号设置注意事项

    防火墙一定要关闭 路由器要映射本地1723和1701端口 tcp协议(个别路由器和网络环境,可以先不设置) pptp配置注意协议的选择:

  4. throws与throw的对比

    1.throws关键字通常被应用在声明方法时,用来指定可能抛出的异常.多个异常可以使用逗号隔开.当在主函数中调用该方法时,如果发生异常,就会将异常抛给指定异常对象.如下面例子所示:public cla ...

  5. 【学习笔记】【C语言】变量的内存分析

    计算机中的内存是以字节为单位的存储空间.内存的每一个字节都有一个唯一的编号,这个编号就称为地址.就好像酒店是以房间为单位的,每个房间都有一个唯一的房号,我们根据房号就能找到对应的房间. 1. 变量的存 ...

  6. How Do I Declare A Block in Objective-C?

    As a local variable: returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...}; As a p ...

  7. WCF之错误和异常

    CLR异常无法跨越服务边界,所有的异常都被封装(序列化)为SOAP Fault,可以让所有平台的用户接收到. SOAP1.1只有Body.1.2中含有Header+Body. 未捕获异常 异常会从逻辑 ...

  8. linux下开发板网络速度测试记录

        由于做的项目对于网络和USB的读写速度有很高的要求,因此新拿回来的板子要测试网络和usb的最佳传输速度.要考虑不少因素,先把我能想到的记录下来.     测试的环境是开发板和ubuntu虚拟机 ...

  9. 济南学习 Day 5 T1 pm

    欧拉函数(phi)题目描述: 已知(N),求phi(N). 输入说明: 正整数N. 输出说明: 输出phi(N). 样例输入: 8 样例输出: 4 数据范围: 对于20%的数据,N<=10^5 ...

  10. 一步一步学习C++

    根据<C++ primer>第五版 总结学习心得. 在实践中,不必全面地使用C++语言的各种特性,而应根据工程的实际情况,适当取舍(譬如动态类型信息,虚拟继承.异常等特性的使用,很值得商榷 ...