【题目】

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. linux系统监控常用工具

    linux系统监控常用工具 一.系统核心工具包(coreutils) 1./bin/df 报告系统的磁盘空间用量 df -h  显示磁盘分区fdisk -l 2./bin/uname 显示系统信息 u ...

  2. VMware Workstation虚拟机使用ISO映像文件

    VMware Workstation虚拟机使用ISO映像文件 VMware Workstation虚拟机使用ISO映像文件

  3. sql的基本查询语句

    --------------------------------------------基本常用查询-------------------------------------- 自己简单练习做了个表. ...

  4. AJAX提交到Handler.ashx一般处理程序返回json数据 (字符串拼接方式)

    <%@ WebHandler Language="C#" Class="Handler" %> using System; using System ...

  5. docker学习笔记(1)

    (1)Docker介绍 关于Docker的介绍,我就不列举出来了.到百度.谷歌搜索.非常多介绍文章.以下我给出官网的介绍:https://www.docker.com/whatisdocker/ (2 ...

  6. Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array

    解题报告 http://blog.csdn.net/juncoder/article/details/38102391 对于给定的数组,取对数组中的一段进行翻转,问翻转后是否是递增有序的. 思路: 仅 ...

  7. Reverse Key Indexes反向索引

    Reverse Key Indexes反向索引A reverse key index is a type of B-tree index that physically reverses the by ...

  8. HTTP概念进阶

    1.什么是回调? 在Java中,就是类A调用类B中的某个方法b,然后类B又在某个时候反过来调用类A中的某个方法a,对于A来说,这个a方法便叫做回调方法 pubilc interface CallBac ...

  9. 从一个小例子认识SQL游标

    1    什么是游标: 关系数据库中的操作会对整个行集起作用. 例如,由 SELECT 语句返回的行集包括满足该语句的 WHERE 子句中条件的所有行. 这种由语句返回的完整行集称为结果集. 应用程序 ...

  10. (转)OpenGL中位图的操作(glReadPixels,glDrawPixels和glCopyPixels应用举例)

    (一)BMP文件格式简单介绍 BMP文件是一种像素文件,它保存了一幅图象中所有的像素.这种文件格式可以保存单色位图.16色或256色索引模式像素图.24位真彩色图象,每种模式种单一像素的大小分别为1/ ...