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 ...
随机推荐
- Ant Design 3.0 使用案例
代码地址如下:http://www.demodashi.com/demo/12309.html 本文适合对象 有过React使用经验. 有过webpack使用经验. 了解node. DEMO使用方式 ...
- android历史
Android一词最早是出如今法国作家维里耶德利尔·亚当1986年发表的<未来夏娃>这部科幻小说中,作者利尔·亚当将外表像人类的机器起名为Android.这就是Android小人名字的由来 ...
- 设计模式之Protocol实现代理模式
使用场合 使用步骤 不使用protocol实现代理 使用protocol实现代理 一.使用场合 A想让B帮忙,就让B代理 A想通知B发生了一些事情,或者传一些数据给B 观察者模式 二.使用步骤 定义一 ...
- PHP第四课 了解经常使用的函数
学习概要: 一.语言结构 二.自己定义函数 三.变量作用域 四.静态变量 五.函数返回值 六.參数 七.默认參数 八.引用參数 九.可变个数函数 十.回调函数 十一.变量函数 十二.递归函数 十三.文 ...
- Mac中遇到的Eclipse连接不上mySql的问题
1.首先我们在eclipse中连接数据库的过程中,遇到的问题就是如上图.开始百度Communications link failure 这几个关键字.得到的结果基本上就是基本配置参数wait_time ...
- HTML5开发移动web应用——SAP UI5篇(8)
本次对之前学习的SAP UI5框架知识进行简单小结.以及重点部分知识的梳理. 1.在UI5使用过程中,命名空间的概念非常重要. 2.一般的sap组件引用格式例如以下: sap.ui.define([ ...
- android利用apkplug框架实现主应用与插件通讯(传递随意对象)实现UI替换
时光匆匆,乍一看已半年过去了,经过这半年的埋头苦干今天最终有满血复活了. 利用apkplug框架实现动态替换宿主Activity中的UI元素.以达到不用更新应用就能够更换UI样式的目的. 先看效果图: ...
- difference between VM, Docker and Vagrant区别
VM: VirtualBox, VMware Docker Vagrant using which you can create VMs or container. It interacts wit ...
- 深入理解JVM:JVM执行时数据区域分类
JVM在运行java程序的过程中会把他所管理的内存划分为若干个不同的数据区域. 这些区域都有各自的用途和创建.销毁时间.有些区域随着虚拟机的启动而存在.有些区域则依赖用户线程的启动和结束而建立和销毁. ...
- UFLDL教程
http://ufldl.stanford.edu/wiki/index.php/UFLDL%E6%95%99%E7%A8%8B