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

最新文章

  1. 新手学习Cocoapods教程
  2. flex布局滑动页面
  3. 022医疗项目-模块二:药品目录的导入导出-对XSSF导出excel类进行封装
  4. iOS开发UIScrollView的底层实现
  5. Android Studio添加jar包
  6. Unity3D之如何创建正确的像素比在屏幕上
  7. Asp.net 提供程序模型
  8. Angular2.js——表单(上)
  9. centos7 下安装zabbix3.0 agent
  10. tpshop linux安装下注意事项
  11. java知识整理
  12. hadoop mahout 算法和API说明
  13. Percona-XtraBackup系列二:备份恢复
  14. WampServer 常见问题
  15. 洛谷P4171 [JSOI2010] 满汉全席 [2-SAT,Tarjan]
  16. ASP.NET MVC4 新手入门教程之九 ---9.查询详情和删除方法
  17. ccentos 7下安装php5.6并使用nginx + php-fpm部署多个不同端口网站
  18. MySQL权限管理创建帐户
  19. Request的Body只能读取一次解决方法
  20. Ubuntu安装中文语言包

热门文章

  1. IE8 placeholder不支持的兼容性处理
  2. Java - 自定义异常(尚学堂第六章异常机制作业计算平均数)
  3. clearfix的用法
  4. Java中子类覆盖父类方法所必须满足的条件
  5. 架构实战项目心得(八):dubbo知识的整理
  6. Centos7 部署.netCore2.0项目
  7. shiro,基于springboot,基于前后端分离,从登录认证到鉴权,从入门到放弃
  8. UNIX 5种I/O模型
  9. Eclipse添加JBOSS支持
  10. 关于display:inline-block布局导致错位问题分析