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

SOLUTION 1:

经典 BFS 题目。如果使用DFS,会超时。

使用队列进行BFS。注意,在判断index越界时,主页君写了2种方法。

(1)可以直接对index在0-x*y之间取。这里有个小的trick,在点在左边缘的时候,index-1会导致我们回到上一行的最右边。当然那个点也是

边缘点,所以不会造成解的错误。

(2)判断点是不是在边缘,判定上下左右还有没有节点。

常出错的点:计算index是i * cols + j  这点要记好了。

 public class Solution {
public void solve(char[][] board) {
if (board == null || board.length == 0 || board[0].length == 0) {
return;
} int rows = board.length;
int cols = board[0].length; // the first line and the last line.
for (int j = 0; j < cols; j++) {
bfs(board, 0, j);
bfs(board, rows - 1, j);
} // the left and right column
for (int i = 0; i < rows; i++) {
bfs(board, i, 0);
bfs(board, i, cols - 1);
} // capture all the nodes.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
} else if (board[i][j] == 'B') {
board[i][j] = 'O';
}
}
} return;
} public void bfs1(char[][] board, int i, int j) {
int rows = board.length;
int cols = board[0].length; Queue<Integer> q = new LinkedList<Integer>();
q.offer(i * cols + j); while (!q.isEmpty()) {
int index = q.poll(); // Index is out of bound.
if (index < 0 || index >= rows * cols) {
continue;
} int x = index / cols;
int y = index % cols; if (board[x][y] != 'O') {
continue;
} board[x][y] = 'B';
q.offer(index + 1);
q.offer(index - 1);
q.offer(index + cols);
q.offer(index - cols);
}
} public void bfs(char[][] board, int i, int j) {
int rows = board.length;
int cols = board[0].length; Queue<Integer> q = new LinkedList<Integer>();
q.offer(i * cols + j); while (!q.isEmpty()) {
int index = q.poll(); int x = index / cols;
int y = index % cols; if (board[x][y] != 'O') {
continue;
} board[x][y] = 'B';
if (y < cols - 1) {
q.offer(index + 1);
} if (y > 0) {
q.offer(index - 1);
} if (x > 0) {
q.offer(index - cols);
} if (x < rows - 1) {
q.offer(index + cols);
}
}
}
}

SOLUTION 2:

附上DFS解法:

 public void dfs(char[][] board, int i, int j) {
int rows = board.length;
int cols = board[0].length; // out of bound or visited.
if (i < 0 || i >= rows || j < 0 || j >= cols) {
return;
} if (board[i][j] != 'O') {
return;
} board[i][j] = 'B'; // dfs the sorrounded regions.
dfs(board, i + 1, j);
dfs(board, i - 1, j);
dfs(board, i, j + 1);
dfs(board, i, j - 1);
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/bfs/Solve.java

LeetCode: 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: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  3. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  4. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  5. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  6. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  7. [leetcode]Surrounded Regions @ Python

    原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...

  8. Leetcode: Surrounded regions

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

  9. LEETCODE —— Surrounded Regions

    Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...

随机推荐

  1. openerp 7.0接收陌生邮件名称显示乱码问题解决方法

      修改代码:addons\mail\mail_thread.py #858 line #msg_dict['email_from'] = decode(message.get('from')) ms ...

  2. 在云主机或vps上用bzr拉OpenERP7.0代码

    前面的文章讲过了用bzr来参与OpenERP开发的过程,其中很重要的一步就是创建本地分支.我在阿里云上建server和web的分支都没有问题,就是addons分支搞了30几次都在中途被kill了. 今 ...

  3. html5之canvas画图 1.写字板功能

     写字板事例:       写字板分析:1.点击鼠标開始写字(onmosedown)2.按下鼠标写字(onmousemove)3.松开鼠标,停下写字(撤销onmousemove事件):       代 ...

  4. java开发工具之myeclipse调优

    -vmargs -Xms512m //堆的最小值-Xmx512m //堆的最大值(两者设置相同,避免运行时的自动扩张)-XX:PermSize=256m //永久代的最小值 -XX:MaxPermSi ...

  5. C# 关闭显示器(显示)

    1.先引入DllImport所在的名称空间 using System.Runtime.InteropServices; 2.引入方法 [DllImport("user32.dll" ...

  6. zookeeper单节点和多节点配置

    单机单节点模式 zookeeper解压, 放到 /opt/zookeeper/下, 新建一个latest的软链 $ latest 将 conf/zoo-sample.cfg 重命名为 zoo.cfg, ...

  7. Emacs 的版本控制功能

    All operations: C-x v + vc-update C-x v = vc-diff C-x v D vc-root-diff C-x v I vc-log-incoming C-x v ...

  8. Linux内核(4) - 内核学习的心理问题

    对于学习来说,无论是在学校的课堂学习,还是这里说的内核学习,效果好或者坏,最主要取决于两个方面——方法论和心理.注意,我无视了智商的差异,这玩意儿玄之又玄,岔开了说,属于迷信的范畴. 前面又是Kern ...

  9. SIPp常用脚本之二:UAS

    看名字就能猜出来,这是作为SIP消息服务端的存在,启动uas,等着接受SIP消息并且给出响应. 一.uas.xml <?xml version="2.0" encoding= ...

  10. 关于FSMC地址线的理解

    http://www.openedv.com/thread-33759-1-1.html (出处: OpenEdv-开源电子网)