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 ...
随机推荐
- CSS3学习笔记——伪类hover
最近看到一篇文章:“Transition.Transform和Animation使用简介及应用展示” ,想看看里面 “不同缓动类效果demo”例子的效果,发现了一个问题如下: .Trans_Bo ...
- 防抖(Debounce)与节流( throttle)区别
http://www.cnblogs.com/ShadowLoki/p/3712048.html http://blog.csdn.net/tina_ttl/article/details/51830 ...
- iOS开发——iOS学习路线
iOS学习路线 版权声明:欢迎转载,请贴上源地址:http://www.cnblogs.com/iCocos/(iOS梦工厂) 一:自学初步学习路线 二:高级完整学习路线 三:完整知识与能力体系 思维 ...
- iOS开发——高级技术精选&底层开发之越狱开发第二篇
底层开发之越狱开发第二篇 今天项目中要用到检查iPhone是否越狱的方法. Umeng统计的Mobclick.h里面已经包含了越狱检测的代码,可以直接使用 /*方法名: * isJailbroken ...
- 彻底解决Android SDK Manager更新慢的问题
Android SDK 下载速度慢,解决方法大概有两种.第一,FQ.这种方法比较彻底,但是要想有稳定的效果还的要花大价钱.第二,有些高人直接给了SDK中各软件的下载地址,直接下载速度非常快,下载后将包 ...
- 不同iOS版本做代码适配__IPHONE_OS_VERSION_MAX_ALLOWED 和 __IPHONE_8_0等专业术语
目前开发只想最低版本支持iOS8了,iOS8以前的就不管了,然后现在iOS9和iOS10出来以后,有些新的API,也有些弃用的API,为了兼容,有时候代码里面需要编写判断不同iOS版本,或者只允许指定 ...
- Android Studio] Gradle项目中添加JNI生成文件(.so文件)
转:http://blog.csdn.net/qiujuer/article/details/24209457 为了适应潮流使用Android Studio还是有半年多了! 对于从Eclipse迁移项 ...
- pentaho saiku 安装全过程
公司希望也开发一套多维分析系统,以解决运营/产品无休止的需求和技术人力不足的矛盾! 一.开发选型: 一.BIRT:易用性差,所以没再使用 二.JasperReport+ireport:文档收费,不支持 ...
- oracle create table(转)
//建测试表 create table dept( deptno number(3) primary key, dname varchar2(10), loc varchar2(13) ); crea ...
- mysql 性能优化 配置优化
http://download.csdn.net/album/detail/1397/2