题目:

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. egit - not authorized

    A. To specify credentials individually for each remote Open Git repositories view, open "Remote ...

  2. C#排序比较

    与C#定义了相等性比较规范一样,C#也定义了排序比较规范,以确定一个对象与另一个对象的先后顺序.排序规范如下 IComparable接口(包括IComparable接口和IComparable< ...

  3. fflua更新-增加对引用的支持

    简介: fflua 发布了有段时间了,很多网友都用了,并且提供了一些很好的反馈.其中一个就是c++接口注册到lua中时,对引用的支持.这样使用起来更加方便. 原有方式: fflua 中注册c++的类用 ...

  4. OllyICE 调试的程序无法处理异常 解决方法

    问题描述 在用OllyICE打开可执行文件时出现如下图所示错误 解决方法 1. 选项 -> 调试设置 , 打开调试选项 2. 切换到 异常 页签 3. 取消勾选 忽略(传递给程序)以下异常: 单 ...

  5. T-SQL 公用表表达式(CTE)

    公用表表达式(CTE) 在编写T-SQL代码时,往往需要临时存储某些结果集.前面我们已经广泛使用和介绍了两种临时存储结果集的方法:临时表和表变量.除此之外,还可以使用公用表表达式的方法.公用表表达式( ...

  6. EventBus使用介绍

    EventBus是Android下高效的发布/订阅事件总线机制.作用是可以代替传统的Intent,Handler,Broadcast或接口函数在Fragment,Activity,Service,线程 ...

  7. C/C++/Qt 统计运行时间

    http://www.cnblogs.com/Romi/archive/2012/04/19/2457175.html 程序中经常需要统计时间,需要统计某项运算的运行时间时,需要计算时间差. 1. C ...

  8. Qt编写自定义控件一开关按钮

    从2010年进入互联网+智能手机时代以来,各种各样的APP大行其道,手机上面的APP有很多流行的元素,开关按钮个人非常喜欢,手机QQ.360卫士.金山毒霸等,都有很多开关控制一些操作,在Qt widg ...

  9. bower 新建.bowerrc文件

    Twitter工程师团队推出了Bower,这是一个针对Web开发的包管理器.该工具主要用来帮助用户轻松安装CSS.JavaScript.图像等相关包,并管理这些包之间的依赖. 随着网页功能变得越来越复 ...

  10. Qt的零碎知识

    1.QObject是所有Qt对象的基类,他给C++的类带来了若干新的功能.使用Q_OBJECT宏能声明一个C++类为一个QObject.如: class Notepad : public QMainW ...