LeetCode130:Surrounded Regions
题目:
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的更多相关文章
- [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- [LintCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 22. Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 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 用了第一种方式, ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- LeetCode: Surrounded Regions 解题报告
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- Cheat (tldr, bropages) - Unix命令用法备忘单
cheat 是一个Unix命令行小工具,用来查询一些常用命令的惯用法(我们都知道,man page阅读起来太累了,常常是跳到最后去看 examples,但并不是所有man pages里面都有examp ...
- [原创]自定义view之:快速开发一款Material Design风格的dialog的开源项目MDDialog
随着google开始主导Material Design风格的设计,越来越多的app开始使用Material Design风格来设计自己的UI.虽然在Android Studio中集成了多种快速开发框架 ...
- Swift 自动布局框架-SnapKit
官方网址:http://snapkit.io/ Github: https://github.com/SnapKit/SnapKit SnapKit is a DSL to make Auto Lay ...
- 关于WPF的退出
如果你在创建项目的时候细心的查看一下项目的结构,你会发现里面有一个App.xaml,一见到App我们知道是应用程序的关键了配置了,当然,WPF的启动窗体也在这里面设置的. 我们可以在App的中配置启动 ...
- apache服务器配置Net的实践
前置: 在xp系统中,打补丁之类或啥子操作引起或多或少的问题,最终导致iis不能使用: 不想装系统...忍着... 最近突发事件导致,需要摸一下apache服务器处理,好吧,那就搜索下吧..... 目 ...
- Python Django 开发 4 ORM
第三篇最后写了,光知道那些基础的查询在项目中是没有什么卵用的,重点是实体关系映射(ORM),今天学习了,来记录一下,关键词:ForeignKey(多对一).OneToOneField(一对一).Man ...
- zmq 学习笔记
0. PUB/SUB, XPUB/XSUB filtering happens at publisher sides when sockets are using a connected protoc ...
- nginx重定向规则入门
nginx重定向规则的入门实例 时间:2015-12-17 15:18:03来源:网络 导读:nginx重定向规则,Nginx的重定向模块HttpRewriteModule的用法说明,nginx重定向 ...
- Knockout 新版应用开发教程之"visible"绑定
"visible" 绑定 用途 DOM元素的显示或者隐藏是根据绑定的值来的,前提是将visible绑定给该元素 例子 <div data-bind="visible ...
- 受限玻尔兹曼机(RBM)学习笔记(一)预备知识
去年 6 月份写的博文<Yusuke Sugomori 的 C 语言 Deep Learning 程序解读>是囫囵吞枣地读完一个关于 DBN 算法的开源代码后的笔记,当时对其中涉及的算法原 ...