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. git查看各个branch之间的关系图

    两种方法: 一.  使用Git log命令 git log --graph --decorate --oneline --simplify-by-decoration --all 说明: --deco ...

  2. 怎么将txt文件转化为html格式的文件?--极为丑陋的方式

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #怎么将txt文件转化为html格式的文件?--极为丑陋的方式 import os #找出所有的txt格式的文 ...

  3. C# winform 安装服务

    一.知识点 1.安装服务 installutil HardwareScanService.exe //安装服务 sc config HardwareScanService type= interact ...

  4. git编译

    Git 是一个自由.开源.高效的分布式版本控制系统(VCS),它是基于速度.高性能以及数据一致性而设计的,以支持从小规模到大体量的软件开发项目.Git 是一个可以让你追踪软件改动.版本回滚以及创建另外 ...

  5. python实现的、带GUI界面电影票房数据可视化程序

    代码地址如下:http://www.demodashi.com/demo/14588.html 详细说明: Tushare是一个免费.开源的python财经数据接口包.主要实现对股票等金融数据从数据采 ...

  6. awk 取列后对数值进行判断取出大于1的数值

    [root@dataline-prod nginx]# tail -2 access.log 122.238.119.177 - - [26/Oct/2018:18:20:25 +0800] &quo ...

  7. SQL plus连接远程Oralce数据库

    如果要连接远程数据库,传统的一定可行的方法是在本地装一个oracle,然后使用“Network Configuration Assistant”配置,之后用PL/SQL Dev连接 oracle官网上 ...

  8. 安装 nvm 遇到的坑

    本篇文章由:http://xinpure.com/encountered-nvm-installation-pits/ 说两句 以前开发都是用最新的 Node 版本,不过难免会有旧项目需要使用低版本做 ...

  9. Eclipse 调试器:零距离接触实战技巧

    http://my.oschina.net/willSoft/blog/37784调试的方法虽然千千万万,但归根结底,就是找到引发错误的代码.Eclipse调试器的目标是让程序员能对本地或远程程序进行 ...

  10. android通过USB使用真机调试程序

    我的机子很老,开启个android模拟器都要好几分钟,但幸亏有个android的真机,这样直接在andriod手机上调试也是一个不错的选择.下面我就介绍 一下使用android手机来调试android ...