【题目】

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'区域触及了矩阵的边界。则这片区域就不算被'X'包围。

【思路】

我们仅仅须要先把触及到边界的'O'区域所有替换成还有一个字母, 比方'M'。矩阵中剩下的'O'区域就肯定是被包围的了。

然后,我们扫描矩阵把'O'替换成'X', 把'M'替换回'O'就可以

    

    区域的遍历,使用DFS或BFS

    DFS要使用递归,当矩阵非常大的时候easy超时

    所以本题使用BFS

【代码】

class Solution {
public: void O2M(vector<vector<char> >&board, int i, int j){
//从board[i][j]開始。遍历'O'区域,把'O'替换成'M'
int rows=board.size();
int cols=board[0].size();
queue<int> qe;
qe.push(i*cols+j);
board[i][j]='M';
while(!qe.empty()){
int pos = qe.front(); qe.pop();
i=pos/cols;
j=pos%cols;
//推断上方
if(i-1>=0 && board[i-1][j]=='O'){
board[i-1][j]='M'; //注意在将相邻元素填到队列中时,须要将它标记为以訪问,否则以后在确定其它节点的'O'相邻位置时,非常有可能又把这个节点增加到队列中。造成死循环。
qe.push((i-1)*cols + j);
}
//推断下方
if(i+1<rows && board[i+1][j]=='O'){
board[i+1][j]='M';
qe.push((i+1)*cols + j);
}
//推断左方
if(j-1>=0 && board[i][j-1]=='O'){
board[i][j-1]='M';
qe.push(i*cols + j-1);
}
//推断右方
if(j+1<cols && board[i][j+1]=='O'){
board[i][j+1]='M';
qe.push(i*cols + j+1);
}
}
} void solve(vector<vector<char>> &board) {
int rows=board.size();
if(rows==0)return;
int cols=board[0].size();
if(cols==0)return; //把临边的'O'区域替换成'M'
//上边
for(int j=0; j<cols; j++){
if(board[0][j]=='O')O2M(board, 0, j);
}
//下边
for(int j=0; j<cols; j++){
if(board[rows-1][j]=='O')O2M(board, rows-1, j);
}
//左边
for(int i=0; i<rows; i++){
if(board[i][0]=='O')O2M(board, i, 0);
}
//右边
for(int i=0; i<rows; i++){
if(board[i][cols-1]=='O')O2M(board, i, cols-1);
} //扫描矩阵。把O替换成X, 把M替换成O
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]=='M')board[i][j]='O';
}
} }
};

LeetCode: Surrounded Regions [130]的更多相关文章

  1. [LeetCode] Surrounded Regions 包围区域

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

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

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

  3. LeetCode: Surrounded Regions 解题报告

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

  4. [leetcode]Surrounded Regions @ Python

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

  5. Leetcode: Surrounded regions

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

  6. LEETCODE —— Surrounded Regions

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

  7. [LeetCode] Surrounded Regions 广度搜索

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

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

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

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

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

随机推荐

  1. Java ClassLoader深入讲解(转)

    当JVM(Java虚拟机)启动时,会形成由三个类加载器组成的初始类加载器层次结构: bootstrap classloader                |       extension cla ...

  2. 【具体数学--读书笔记】1.1 The Power of Hanoi

    这一节借助汉诺塔问题引入了"Reccurent Problems". (Reccurence, 在这里解释为“the solution to each problem depend ...

  3. 能上QQ无法打开网页

    能上QQ无法上网电脑故障解决方法 Winsock协议配置故障解决方法 第1步 :单击开始菜单中的运行,并在打开的运行窗口中键入“cmd”并回车确定,打死命令提示符窗口. 第2步 :在打开的命令提示符窗 ...

  4. 类加载器与methodinterceptor接口

    类加载器: JVM将类加载过程分为三个步骤: 装载(Load):加载二进制文件 链接(Link)进行了验证:验证文件准确性 准备:将静态变量进行分配内存,初始化其默认值. 解析:符号引用转换为直接引用 ...

  5. GitHub以及Git学习 持续编辑学习中

    官网地址: http://www.worldhello.net/gotgithub/01-explore-github/030-explore-github.html 1 加入github, http ...

  6. animate CSS动画程序接口(仅Chrome可用)

    jQuery中很早就提供了animate方法,使用它可以很方便地实现一些简单动画效果.后来CSS3中也提供了animation用于动画效果制作,但CSS本身的可操作性太差,所以用起来并不方便.现在最新 ...

  7. 调用Response.Redirect 捕获异常 解决办法(摘抄)

    如果使用 Response.End.Response.Redirect 或 Server.Transfer 方法,将出现 ThreadAbortException 异常.您可以使用 try-catch ...

  8. Android 四大组件之Activity生命周期

    写这篇博文之前,已经对android有一定的了解和认识.这篇博文主要讲述android的Activity的生命周期,这是android开发者必须掌握的知识.android的Activity组件拥有7个 ...

  9. 由chkconfig 引发的联想——怎么查看程序是否已经安装/成功安装

    由chkconfig 引发的联想--怎么查看程序是否已经安装/成功安装 某天需要运行chkconfig,root登录依然找不到该命令. [root@localhost ~]# chkconfig ba ...

  10. iOS集成支付宝

    需要企业和支付宝签约这个是需要审核的[3天左右   以邮件形式告知你] 使用支付宝进行一个完整的支付功能,大致有以下步骤: 1>先与支付宝签约,获得商户ID(partner)和账号ID(sell ...