leetcode-surrounded regions-ZZ
Problem Statement (link):
'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
X X X X
X X X X
X X X X
X O X X
Analysis:
Rather than recording the 2D positions for any scanned 'O', a trick is to substitute any border 'O's with another character - here in the Code I use 'Y'. And scan the board again to change any rest 'O's to 'X's, and change 'Y's back to 'O's.
We start searching 'O' from the four borders. I tried DFS first, the OJ gives Runtime error on the 250x250 large board; In the Sol 2 below, I implement BFS instead, and passed all tests.
The time complexity is O(n^2), as in the worst case, we may need to scan the entire board.
Code:
1, DFS
class Solution {
public:
// dfs - Runtime error on large board 250x250
void dfs(vector<vector<char>> &board, int r, int c) {
if (r<||r>board.size()-||c<||c>board[].size()-||board[r][c]!='O')
return;
board[r][c]='Y';
dfs(board, r-, c);
dfs(board, r+, c);
dfs(board, r, c-);
dfs(board, r, c+);
}
void solve(vector<vector<char>> &board) {
if (board.empty() || board.size()< || board[].size()<)
return;
int r=board.size();
int c=board[].size();
// dfs from boundary to inside
for (int i=; i<c; i++) {
if (board[][i]=='O')
dfs(board, , i); // first row
if (board[c-][i]=='O')
dfs(board, c-, i); // last row
}
for (int i=; i<board.size(); i++) {
if (board[i][]=='O')
dfs(board, i, ); // first col
if (board[i][c-])
dfs(board, i, c-); // last col
}
// scan entire matrix and set values
for (int i=; i<board.size(); i++) {
for (int j=; j<board[].size(); j++) {
if (board[i][j]=='O')
board[i][j]='X';
else if (board[i][j]=='Y')
board[i][j]='O';
}
}
}
};
2, BFS
class Solution {
public:
void solve(vector<vector<char>> &board) {
if (board.empty() || board.size()< || board[].size()<)
return;
int r=board.size();
int c=board[].size();
// queues to store row and col indices
queue<int> qr;
queue<int> qc;
// start from boundary
for (int i=; i<c; i++) {
if (board[][i]=='O') { qr.push(); qc.push(i); }
if (board[r-][i]=='O') { qr.push(r-); qc.push(i); }
}
for (int i=; i<r; i++) {
if (board[i][]=='O') { qr.push(i); qc.push(); }
if (board[i][c-]=='O') { qr.push(i); qc.push(c-); }
}
// BFS
while (!qr.empty()) {
int rt=qr.front(); qr.pop();
int ct=qc.front(); qc.pop();
board[rt][ct]='Y';
if (rt->= && board[rt-][ct]=='O') { qr.push(rt-); qc.push(ct); } //go up
if (rt+<r && board[rt+][ct]=='O') { qr.push(rt+); qc.push(ct); } // go down
if (ct->= && board[rt][ct-]=='O') { qr.push(rt); qc.push(ct-); } // go left
if (ct+<c && board[rt][ct+]=='O') { qr.push(rt); qc.push(ct+); } // go right
}
// scan entire matrix and set values
for (int i=; i<board.size(); i++) {
for (int j=; j<board[].size(); j++) {
if (board[i][j]=='O') board[i][j]='X';
else if (board[i][j]=='Y') board[i][j]='O';
}
}
}
};
http://justcodings.blogspot.com/2014/07/leetcode-surrounded-regions.html
leetcode-surrounded regions-ZZ的更多相关文章
- [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 ...
- LeetCode: Surrounded Regions 解题报告
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- [leetcode]Surrounded Regions @ Python
原题地址:https://oj.leetcode.com/problems/surrounded-regions/ 题意: Given a 2D board containing 'X' and 'O ...
- Leetcode: Surrounded regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- LEETCODE —— Surrounded Regions
Total Accepted: 43584 Total Submissions: 284350 Difficulty: Medium Given a 2D board containing 'X' a ...
- LeetCode: Surrounded Regions [130]
[题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...
- [LeetCode] Surrounded Regions 广度搜索
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- [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 (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- 如何在react&webpack中引入图片?
在react&webpack项目中需要引入图片,但是webpack使用的模块化的思想,如果不进行任何配置,而直接在jsx或者是css中使用相对路径来使用就会出现问题,在webpack中提供了u ...
- Git学习系列之Git产生的背景
不多说,直接上干货! 史上最浅显易懂的Git教程! 为什么要编写这个教程?因为我在学习Git的过程中,买过书,也在网上Google了一堆Git相关的文章和教程,但令人失望的是,这些教程不是难得令人发指 ...
- MySQL获取字段的片段
如表中有很多这样的数据: TEST-123,TEST-III 这种以 TEST开头的数据,为了统计其总数 可以使用mysql自带的方法 substring_index()方法 第一个参数是列的内容, ...
- GIT 恢复单个文件到历史版本
首先查看该文件的历史版本信息:git log <file> 恢复该文件到某个历史版本:git reset 版本号 <file> 检出改文件到工作区:git checkout - ...
- 深入redis内部--初始化服务器
初始化服务器代码如下: void initServer() { int j; signal(SIGHUP, SIG_IGN); signal(SIGPIPE, SIG_IGN); setupSigna ...
- 使用webgl(three.js)创建3D机房(升级版)-普通机房
序: 目前市面上的数据中心主要分两大类,一类属于普通数据中心,机柜按照XY轴 有序排放,一类属于微模块集合的数据中心,多个机柜组合而成的微模块. 本节课主要详细讲解普通数据中心的可视化展示,浏览器直 ...
- 深入理解JavaScript系列(46):代码复用模式(推荐篇)
介绍 本文介绍的四种代码复用模式都是最佳实践,推荐大家在编程的过程中使用. 模式1:原型继承 原型继承是让父对象作为子对象的原型,从而达到继承的目的: function object(o) { fun ...
- Git中.gitignore, 忽略追踪
在目录下 创建: .gitignore文件,将不需要被追踪的文件地址, 写在该文件中, 此时git软件就不会追踪列出的文件进行版本同步: windows不允许创建没有文件名的文件,可以用编辑器创建.g ...
- TSM简介
转自:https://www.cnblogs.com/Study-Blog/p/8644376.html Tivoli TSM产品功能详述Tivoli TSM(Tivoli Storage Manag ...
- easyui导出当前datagrid数据(Word)
JS代码可参考http://www.cnblogs.com/mu1516633121/p/7753423.html 同样是winform架构下应用到Aspose.Words来读写Word文档 其中Se ...