leetcode 130 Surrounded Regions(BFS)
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 题解:这道题测试数据卡的很严。一开始用dfs爆栈了,后来改成bfs又超时了。。最后改进了下:先查边上有没有‘O’,和边上连着的肯定不会被围住。把这些都标记出来以后,剩下的里面的‘O’必然都要变成‘X’。
class Solution {
public:
int n,m;
int dx[]={,,,-};
int dy[]={,-,,};
vector<vector<char> >flag;
void bfs(vector<vector<char> >& board,int x,int y){
queue<pair<int,int> >q;
q.push(make_pair(x,y));
flag[x][y]=;
while(!q.empty()){
int qx=q.front().first;
int qy=q.front().second;
q.pop();
for(int i=;i<;i++){
int tx=qx+dx[i];
int ty=qy+dy[i];
if(tx>=&&tx<n&&ty>=&&ty<m&&flag[tx][ty]==&&board[tx][ty]=='O'){
q.push(make_pair(tx,ty));
flag[tx][ty]=;
}
}
}
}
void solve(vector<vector<char> >& board) {
if(board.empty()){
return ;
}
n=board.size();
m=board[].size();
flag.resize(n);
for(int i=;i<n;i++){
flag[i].resize(m,);
}
for(int i=;i<m;i++){
if(board[][i]=='O'&&flag[][i]==){
bfs(board,,i);
}
if(board[n-][i]=='O'&&flag[n-][i]==){
bfs(board,n-,i);
}
}
for(int i=;i<=n-;i++){
if(board[i][]=='O'&&flag[i][]==){
bfs(board,i,);
}
if(board[i][m-]=='O'&&flag[i][m-]==){
bfs(board,i,m-);
}
}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(board[i][j]=='O'&&flag[i][j]==){
board[i][j]='X';
}
}
}
}
};
leetcode 130 Surrounded Regions(BFS)的更多相关文章
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- Leetcode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- Java for LeetCode 130 Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- Leetcode 130 Surrounded Regions DFS
将内部的O点变成X input X X X XX O O X X X O XX O X X output X X X XX X X XX X X XX O X X DFS的基本框架是 void dfs ...
- 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 用了第一种方式, ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 【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 ...
随机推荐
- Mybatis无法扫描到mapper.xml文件
在Mybatis中默认扫描与mapper包同路径下的xml,resource文件的文件夹名称不能一次性创建,如com.baidu.mapper需要创建3次 这里如果是idea开发工具,一次创建与分开创 ...
- HTML经典标签用法
1.marquee属性的使用说明 <marquee> ... </marquee>移动属性的设置 ,这种移动不仅仅局限于文字,也可以应用于图片,表格等等 鼠标属性 onMo ...
- 嵌入式开发之davinci--- 8148/8168/8127 中的xdc 简介
XDC是TI公司为嵌入式实时系统可重用软件组件(在XDC里被成为packages,以下成为包)制定的一套标准.它包括一些有用的工具,标准的API函数,静态配置文件和打包(packaging)操作.XD ...
- POJ2407_Relatives【欧拉phi函数】【基本】
Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11422 Accepted: 5571 Descriptio ...
- 安装Redis 非结构化数据库
1.官网下载安装包 1) 首先在Redis官网下载安装包: http://redis.io/download(redis-4.0.9.tar.gz) 2.在/usr/local/创建一个redi ...
- POJ3182 The Grove[射线法+分层图最短路]
The Grove Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 904 Accepted: 444 Descripti ...
- jvm堆查看
jps查看jvm的进程号 jmap -histo:live [进程号] >log.txt dump jvm堆.
- tcp/ip (网络通讯协议)
介绍 TCP: 传输控制协议, IP: 网际协议, TCP/IP: 供已连接互联网的计算机之间进行通信的通信协议 在tcp/ip内部 , 包含一系列处理数据通信的协议: tcp.udp.icmp.dh ...
- (转) 在linux网络UDP通信中,关于客户端是否绑定的理解
最近在做一个实例,是用RTSP协议完成.服务器已经有了,只需要把客户端做好就行了,在做的过程中发现了一些问题,就是关于UDP客户端是否绑定的问题. 也许大家在书上看到的大多都是说UDP客户端不需要绑定 ...
- 蓝牙 CTS 测试
安装蓝牙测试安装包 之后 . 安卓包名字 android-cts-6.0_r19-linux_x86-x86.zip 解压之后 /cts/android-cts/tools/ 目录下 运行 ./ ...