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. 解决php中redis client进行subscribe操作出现timeout的问题

    出现该问题的原因是poll设置接收超时所致,这个超时默认设置60s 设置Redis::OPT_READ_TIMEOUT配置项: 解决方法如下: <?php $redis = new Redis( ...

  2. Mac outlook设置自动回复

    outlook是公司必不可少的软件, 在mac下开发,当然用的是mac版的outlook,今天介绍一下如何设置mac下outlook的自动回复. 有两种方式的帐号,一种是Exchange accoun ...

  3. retain和copy还有assign的区别

    1. 接触过C,那么假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给 (assign)了b.此时a和b指向同一块内存,请问当a不再需 ...

  4. 七天学会ASP.NET MVC (二)——ASP.NET MVC 数据传递 【转】

    http://www.cnblogs.com/powertoolsteam/p/MVC_two.html 通过第一天的学习之后,我们相信您已经对MVC有一些基本了解. 本节所讲的内容是在上节的基础之上 ...

  5. DNS_主从服务_详细搭建&&配置

    DNS主从 安装环境: 三台dns服务器如下: 系统:均为centos7 dns_master:192.168.169.194 dns_slave-1:192.168.169.195 dns_slav ...

  6. 【重点突破】——第三方绘图工具FusionCharts.js的使用详解

    一.引言 项目组中,经常会因为绘制图表的繁杂度,衡量会不会使用第三方绘图工具,如果自己做很困难,成本使用高于第三方绘图工具库,就会使用.很多人使用的是Chart.js,因为它是免费使用的,不过,缺点就 ...

  7. Java数组去掉反复的方法集

    经经常使用到,有时候不仅仅是简单的基本类型,那种能够用set集合去重,好多时间用到的是我们自己定义的类型,以下举个样例(我这儿就那int举例了): 方法一. 这样的类似与选择排序算法,首先我们取i值, ...

  8. element的el-tabs控制,以及el-select 多选默认值

    一.el-tabs 1.element自己已经封装好了,当切换时v-model的值自动切换为el-tabs-pane的name对应的值. 如下: <el-tabs v-model='active ...

  9. sql数据分页

    方法一.直接限制返回区间 (只适应于mysql) SELECT * FROM table WHERE 查询条件 ORDER BY 排序条件 LIMIT ((页码-1)*页大小),页大小; 优点:写法简 ...

  10. sakila演示数据库安装

    下载地址:https://dev.mysql.com/doc/index-other.html 安装帮助文档:https://dev.mysql.com/doc/sakila/en/sakila-in ...