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)的更多相关文章

  1. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  2. Leetcode 130. Surrounded Regions

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  3. Java for LeetCode 130 Surrounded Regions

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  4. 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 ...

  5. 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 用了第一种方式, ...

  6. 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...

  7. [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 ...

  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. 硬件问题大杂烩&Coffee lake框图

    PCB阻抗控制 https://www.cnblogs.com/lifan3a/articles/6095372.html 1.高速差分信号串联AC耦合电容什么请况下要做镂空处理: (1)为了阻抗匹配 ...

  2. HTTP状态码中301与302的区别

    301 Moved Permanently 被请求的资源已永久移动到新位置,并且将来任何对此资源的引用都应该使用本响应返回的若干个URI之一.如果可能,拥有链接编辑功能的客户端应当自动把请求的地址修改 ...

  3. 怎样替换jar包的指定文件

    在做(或修改别人的)项目的时候,可能遇到要修改调用的的jar包内的类属性或方法的问题.在eclipse或是其他的IDE中是无法直接修改的,所以需要一个解压jar-->修改文件-->编译-- ...

  4. 10-客户端防表单重复提交和服务器端session防表单重复提交

    /****************************************************DoFormServlet********************************** ...

  5. os如何处理键盘的所有按键,显示or不显示,显示是如何显示

    [0]README 0.1) source code and text decription are from orange's implemention of a os , and for comp ...

  6. Python中文编码过程中遇到的一些问题

    首先,要明确encode()和decode()的差别 encode()的作用是将Unicode编码的字符串转换为其它编码格式. 比如:st1.encode("utf-8")  这句 ...

  7. Xcode6中如何对scrollview进行自动布局(autolayout)

    本文转载至 http://www.cocoachina.com/ios/20141011/9871.html XCodeAutolayoutscrollView     Xcode6中极大的增强了IB ...

  8. android菜鸟学习笔记16----Android项目打包安装过程(Run as Android Application)

    右击项目名称,Run as Android Appication之后,Android项目打包安装过程: 1.打包生成.apk文件: 1)把源码中的.java文件编译生成.class文件 2)将所有的. ...

  9. android菜鸟学习笔记6----android布局(一)

    Android应用的UI组件都是继承自View类,View类表示的就是一个空白的矩形区域.常用的组件如TextView.Button.EditText等都直接或间接继承自View. 此外,View还有 ...

  10. HTML 学习笔记 JQuery(DOM 操作3)

    设置和获取HTML 文本 和 值 1.html()方法 类似于JavaScript中的innerHTML属性,可以用来读取或者设置某个元素中的HTML内容 例子 <html> <he ...