题目:

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
解题思路:
首先在遍历最外面的四条边,如果遇到O,则表示有出路,这时,以找到的O点为起点,采用BFS或DFS进行遍历,找到与其他与该O点相邻的O点,然后将其置为*,第一步处理完后,再重新遍历整个矩阵,将为*的点置为O,为O的点置为X即可。
实现代码:
#include <iostream>
#include <vector>
#include <queue>
using namespace std; /*
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
*/ class Solution {
public:
void solve(vector<vector<char>> &board) {
if(board.empty() || board[0].empty())
return ;
int rows = board.size();
int cols = board[0].size();
for(int i = 0; i < rows; i++)
{
if(board[i][0] == 'O')
bfs(i, 0, board, rows, cols);
if(board[i][cols-1] == 'O')
bfs(i, cols-1, board, rows, cols);
}
for(int j = 0; j < cols; j++)
{
if(board[0][j] == 'O')
bfs(0, j, board, rows, cols);
if(board[rows-1][j] == 'O')
bfs(rows-1, j, board, rows, cols);
} 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] == '*')
board[i][j] = 'O'; } void bfs(int i, int j, vector<vector<char>> &board, int rows, int cols)
{
queue<pair<int, int>> qu;
qu.push(make_pair(i, j));
while(!qu.empty())
{
pair<int, int> p = qu.front();
qu.pop();
int ii = p.first;
int jj = p.second;
if(ii < 0 || ii >= rows || jj < 0 || jj >= cols || board[ii][jj] != 'O')
continue;
board[ii][jj] = '*';
qu.push(make_pair(ii, jj-1));
qu.push(make_pair(ii, jj+1));
qu.push(make_pair(ii-1, jj));
qu.push(make_pair(ii+1, jj)); }
}
}; int main(void)
{
return 0;
}

LeetCode130:Surrounded Regions的更多相关文章

  1. [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions

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

  2. [LeetCode] Surrounded Regions 包围区域

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

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

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

  4. [LintCode] Surrounded Regions 包围区域

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

  5. 22. Surrounded Regions

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

  6. Surrounded Regions

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

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

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

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

  9. LeetCode: Surrounded Regions 解题报告

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

随机推荐

  1. CentOS 7.0系统安装配置LAMP服务器(Apache+PHP+MariaDB)

    CentOS 7.0接触到的用户是比较少的,今天看了站长写了一篇关于centos7中安装配置LAMP服务器的教程,下面我把文章稍加整理一下转给大家学习交流,希望例子能给各位带来帮助哦.   cento ...

  2. [leetcode]Second Highest Salary

    找第二大 # Write your MySQL query statement below SELECT MAX(Salary) FROM Employee WHERE Salary NOT IN ( ...

  3. 关于Parallel.For/Foreach并行方法中的localInit, body, localFinally使用

    对集合成员的操作往往可以通过并行来提高效率,.NET Parallel类提供了简单的方法来帮助我们实现这种并行,比如Paralle.For/ForEach/Invoke方法. 其中,For/ForEa ...

  4. 使用Javascript来创建一个响应式的超酷360度全景图片查看幻灯效果

    360度的全景图片效果常常可以用到给客户做产品展示,今天这里我们推荐一个非常不错的来自Robert Pataki的360全景幻灯实现教程,这里教程中将使用javascript来打造一个超酷的全景幻灯实 ...

  5. Adobe flash player更新失败

  6. 为应用程序池 'DefaultAppPool' 提供服务的进程无法响应Ping

    打开Windows 2008 的事件查看器,应用警告提示: 为应用程序池 'DefaultAppPool' 提供服务的进程无法响应 Ping.进程 ID 是 '2144'. 注意: 需要重新注册一vb ...

  7. HBase 在HDFS 上的目录树

         总所周知,HBase 是天生就是架设在 HDFS 上,在这个分布式文件系统中,HBase 是怎么去构建自己的目录树的呢? 这里只介绍系统级别的目录树. 一.0.94-cdh4.2.1版本 系 ...

  8. mysql日期类型默认值'0000-00-00'容错处理

    mysql日期默认值'0000-00-00'惹的祸 .net连mysql数据库时,如果表里有字段是日期型且值是‘0000-00-00’时,会报错.在C#里面日期不可能是那样的.或许是最小日期定义的差别 ...

  9. joomla allvideo 去掉embed share

    文件位置plugins/content/jw_allvideos/jw_allvideos/includes/sources.php找到以下代码 jwplayer('avID_{SOURCEID}') ...

  10. mongodb备份恢复

    注意:在备份文件存放目录的选择上有这样一个条件,文件存放目录不管有多深,都只能有它一个文件:(即除了备份文件之外只能存在文件夹,这个条件约束其整个目录树) 数据备份 : /mongodump --ho ...