Surrounded Regions
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
Surrounded Regions的更多相关文章
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- 【leetcode】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [LintCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 22. Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 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 用了第一种方式, ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- poj1328
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 73588 Accepted: 16 ...
- C++之vector用法
1.插入配对 std::vector<pair<int,int> > w; w.push_back(make_pair<int,int>(f,s) ); cout ...
- ListViewDemo
ListView Layout示例:MainActivity.java中定义待显示的数据countryArray, 在activity_main中定义ListView,activity_listvie ...
- mvc路由规则相关
1,可以创建多条路由规则,每条路由规则的name属性不能相同 2,路由规则是有顺序的,如果被前面的规则匹配了,那么后面的规则就没有机会了 下面是一条路由规则的代码 routes.MapRoute( n ...
- ubuntu将默认中文改成英文
找到/etc/default/locale下面的文件,使用vim打开 将看到内容为: LANG=”zh_CN.UTF-8″ LANGUAGE=”zh_CN:zh” 改为 LANG=”en_US.UTF ...
- Markdown 学习资源
语法 Mastering Markdown Markdown Cheatsheet pandoc 在线预览工具
- 在Linux下进行磁盘分区
1. 分区前的规划 2. 查看本机上的磁盘信息 3. 对第二个磁盘进行交换式分区操作(输入m为帮助信息) 图 1:n为新建分区 图 2:p为 ...
- UINavigationController与UITabbarController的样式
之前虽然也手写过这两中视图控制器,但是更多的还是使用SB来创建,最近发现了一些问题,现在总结一下. 1.改变UINavigationBar的颜色 在UINavigationController中,之前 ...
- 如何让R代码按计划执行
应用场景:你编写了R代码,每天对提交的数据进行分析,你希望它你吃饭的时候执行完毕,生成图表. 那么你需要安装taskscheduleR的包. 怎么操作,看帮助呗.
- 20141128—JavaScript对象
JavaScript 中的所有事物都是对象:字符串.数值.数组.函数... String 对象的 length 属性来获得字符串的长度: var message="Hello World!& ...