【题目】

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. 知道版本对于出0day后批量攻击dedecms有非常大的帮助,先判断版本再选择相应exp,效率大增

    需要知道织梦网站版本URL路径后面添加/data/admin/ver.txt 例如:http://www.dedecms.com/data/admin/ver.txt 20100708是最新版本5.6 ...

  2. linux之SQL语句简明教程---LIKE

    LIKE 是另一个在 WHERE 子句中会用到的指令.基本上,LIKE 能让我们依据一个套式 (pattern) 来找出我们要的资料.相对来说,在运用 IN 的时候,我们完全地知道我们需要的条件:在运 ...

  3. poj 1328 Radar Installation_贪心

    翻出一年多前的代码看,发现以前的代码风格很糟糕 题意:给你n个点 m为圆的半径,问需要多少个圆能把全部点圈到 #include <iostream> #include <algori ...

  4. 彩虹vpn免费10分钟

    账号:rainbowvpn password:10fenzhong server地址:t.chqvpn.com l2tp密钥:123456

  5. [week1]每周总结与工作计划

    实在不想说这句俗话,因为实在太俗了.但是俗话说,“吾日三省吾身”,我虽然难以做到每天反省那么多次,但是每周来一次就很不错了.于是我决定: 从这周开始准备每周都写每周总结与工作计划. 很好,就这样说定了 ...

  6. asp.net application

    Application 对象用于存储和访问来自任何页面的变量,类似于 session 对象.不同之处在于,所有的用户分享一个 Application 对象,而 session 对象和用户的关系是一一对 ...

  7. 点击TextView浏览器打开指定网页

    直接上代码: /** * 点击跳转到版权页面 */ private void getCopyRight() { // TODO Auto-generated method stub TextView ...

  8. PHP文件访问技术

    <?php $file=fopen("test.txt","r"); //以只读方式打开test.txt $char=fgetc($file); echo ...

  9. PHP面试题之文件目录操作

    获取文件后缀,遍历目录层次 /** * 5种方式获取文件后缀名 * 这道题其实就是考函数substr() strrchr() array_pop() strrpos() strpos() strrev ...

  10. python---连接MySQL第五页

    Connector/Python Connection Arguments A connection with the MySQL server can be established using ei ...