首页 技术 正文
技术 2022年11月15日
0 收藏 568 点赞 2,391 浏览 2555 个字

Problem Statement (link):

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

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 250×250 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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838