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',找出所有被'X'包围的'O',并替换成'X'。

解题思路:

解法一:从边开始,找出在四条边上的O,以BFS的方式找出所有与边上的O连通的O,这些O都是不被包围的,将这些O替换为某个特殊字符,遍历完之后,将O替换为X,将特殊字符替换为O。

解法二:直接用BFS遍历,找到一个O之后,加入BFS队列,找出与之连通的所有的O同时判断这些O是否被包围(即是否位于某条边上),将这些O加入一个List,每一个连通的O区域用一个surround布尔变量表示这个连通区域是否是被surround的,如果是就把这些O替换为X,否则不替换并继续遍历其他。

注意:这里因为要把i、j下标放入队列或者结果集,一开始我使用String类型key= i+"_"+j 来处理,后来看到别人key = i * colLen + j ; colLen是棋盘的列的数量,然后用key/colLen,key%colLen来得到i,j下标效率更高。

Talk is cheap>>

解法一:

  public void solve(char[][] board) {
Queue<Integer> queue = new ArrayDeque<>();
int rowLen = board.length;
if (rowLen == 0)
return;
int colLen = board[0].length;
int[][] adj = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
for (int i = 0; i < rowLen; i++) {
for (int j = 0; j < colLen; j++) {
          //如果O在四条边上
if (board[i][j] == 'O' && (i == 0 || j == 0 || i == rowLen - 1 || j == colLen - 1)) {
int key = i * colLen + j;
board[i][j] = '1';
queue.add(key);
while (!queue.isEmpty()) {
              //BFS遍历与边上的O连通的O
key = queue.poll();
int x = key / colLen;
int y = key % colLen;
for (int k = 0; k < 4; k++) {
int adj_x = x + adj[k][0];
int adj_y = y + adj[k][1];
if (adj_x >= 0 && adj_y >= 0 && adj_x < rowLen && adj_y < colLen) {
if (board[adj_x][adj_y] == 'O') {
int pos = adj_x * colLen + adj_y;
board[adj_x][adj_y]='1';
queue.add(pos);
}
}
}
}
}
}
}
for (int i = 0; i < rowLen; i++) {
for (int j = 0; j < colLen; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
} else if (board[i][j] == '1') {
board[i][j] = 'O';
}
}
}
}

解法二:

  public void solve(char[][] board) {
Queue<Integer> queue = new ArrayDeque<>();
int rowLen = board.length;
if (rowLen == 0) {
return;
}
int[][] adj = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
int colLen = board[0].length;
boolean[][] visited = new boolean[rowLen][colLen];
for (int i = 0; i < rowLen; i++) {
for (int j = 0; j < colLen; j++) {
if (!visited[i][j] && board[i][j] == 'O') {
            //标准的BFS遍历
boolean surround = true;
List<Integer> toHandle = new ArrayList<>();
queue.add(i * colLen + j);
while (!queue.isEmpty()) {
int key = queue.poll();
toHandle.add(key);
int x = key / colLen;
int y = key % colLen;
for (int k = 0; k < 4; k++) {
                //检查O的上下左右
int adj_x = x + adj[k][0];
int adj_y = y + adj[k][1];
if (adj_x >= 0 && adj_y >= 0 && adj_x < rowLen && adj_y < colLen) {
                  //都不在边上
if (board[adj_x][adj_y] == 'O' && !visited[adj_x][adj_y]) {
int pos = adj_x * colLen + adj_y;
queue.add(pos);
visited[adj_x][adj_y] = true;
}
} else {
                  //有一个在边上,置surround=false
surround = false;
}
}
}
if (surround) {
for (int key : toHandle) {
board[key / colLen][key % colLen] = 'X';
}
}
}
}
}
}

Surrounded Regions——LeetCode的更多相关文章

  1. Surrounded Regions leetcode java

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

  2. Surrounded Regions - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 https://leetcode.com/problems/surrounded-regions/ 注意点 边缘不算包围'O' 解法 解法一:dfs.找处 ...

  3. [LeetCode] Surrounded Regions 包围区域

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

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

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

  5. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  6. Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions)

    Leetcode之深度优先搜索(DFS)专题-130. 被围绕的区域(Surrounded Regions) 深度优先搜索的解题详细介绍,点击 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O) ...

  7. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  8. 【leetcode】Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  9. 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 用了第一种方式, ...

随机推荐

  1. Linux驱动开发学习的一些必要步骤

      1. 学会写简单的makefile 2. 编一应用程序,可以用makefile跑起来 3. 学会写驱动的makefile 4. 写一简单char驱动,makefile编译通过,可以insmod, ...

  2. 第二篇:智能电网(Smart Grid)中的数据工程与大数据案例分析

    前言 上篇文章中讲到,在智能电网的控制与管理侧中,数据的分析和挖掘.可视化等工作属于核心环节.除此之外,二次侧中需要对数据进行采集,数据共享平台的搭建显然也涉及到数据的管理.那么在智能电网领域中,数据 ...

  3. Linux禁止ping服务

    ping是一个通信协议,是ip协议的一部分,tcp/ip 协议的一部分.利用它可以检查网络是否能够连通,用好它可以很好地帮助我们分析判定网络故障.应用格式为:Ping IP地址.但服务启用ping有时 ...

  4. 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之三

    /** ****************************************************************************** * @author    暴走的小 ...

  5. Python之路,Day21 - 常用算法学习

    Python之路,Day21 - 常用算法学习   本节内容 算法定义 时间复杂度 空间复杂度 常用算法实例 1.算法定义 算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的 ...

  6. 【VB】StrConv函数 vbUnicode用法

    [VB]StrConv函数 StrConv(string, conversion, LCID) vbUnicode 64 根据系统的缺省码页将字符串转成Unicode. vbFromUnicode 1 ...

  7. C#—集合(Collection)

    1.栈(stack<T>) using System; using System.Collections.Generic; using System.Linq; using System. ...

  8. CI 模型的使用M与C之间的调用

    CI是PHP一个比较轻,并且好用的一个框架 分层也是分的比较清晰的一个 这里先展示MODEL 放在application/models 目录下面user_model.php <?php clas ...

  9. Android设备 cocos2dx 骨骼动画注册事件播放音效,退到后台再返回黑屏问题

    最近遇到一个cocos2dx 骨骼动画注册事件播放音效,在骨骼动画播放的时候,按HOME键退到桌面,再次打开游戏的时候,会黑屏. 解决办法如下,可能不是太完美,至少解决了大部分问题. 1.在org.c ...

  10. linux common command.

    Stopping & Starting shutdown -h now – Shutdown the system now and do not reboothalt – Stop all p ...