题目:

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

代码:

class Solution {
public:
void solve(vector<vector<char>>& board) {
const int ROW = board.size();
if ( ROW< ) return;
const int COL = board[].size();
// first row
for ( int i = ; i < COL; ++i )
{if ( board[][i]=='O') { board[][i] = 'M'; Solution::bfs(board, , i, ROW, COL); }}
// last row
for ( int i = ; i < COL; ++i )
{if ( board[ROW-][i]=='O' ) { board[ROW-][i] = 'M'; Solution::bfs(board, ROW-, i, ROW, COL); }}
// first col
for ( int i = ; i < ROW; ++i )
{if ( board[i][]=='O' ) { board[i][] = 'M'; Solution::bfs(board, i, , ROW, COL); }}
// last col
for ( int i = ; i < ROW; ++i )
{if ( board[i][COL-]=='O' ) { board[i][COL-]='M'; Solution::bfs(board, i, COL-, ROW, COL); }}
// flipping surrounded regions
for ( int i = ; i < ROW; ++i ){
for ( int j = ; j < COL; ++j ){
if ( board[i][j]=='O' ) { board[i][j]='X'; continue; }
if ( board[i][j]=='M' ) { board[i][j]='O'; continue; }
}
}
}
static void bfs(vector<vector<char> >& board, int row, int col, int ROW, int COL )
{
queue<pair<int, int> > que;
que.push(make_pair(row, col));
while ( !que.empty() ){
int r = que.front().first, c = que.front().second;
que.pop();
// up left down right
if ( r-> && board[r-][c]=='O' ) { board[r-][c]='M'; que.push(make_pair(r-, c)); }
if ( c-> && board[r][c-]=='O') { board[r][c-]='M'; que.push(make_pair(r, c-)); }
if ( r+<ROW- && board[r+][c]=='O' ) { board[r+][c]='M'; que.push(make_pair(r+, c)); }
if ( c+<COL- && board[r][c+]=='O' ) { board[r][c+]='M'; que.push(make_pair(r, c+)); }
}
}
};

tips:

参考的BFS的思路(http://yucoding.blogspot.sg/2013/08/leetcode-question-131-surrounded-regions.html

先说大体思路

这个思路正好与常规的思路逆过来:

1. 常规的思路是从wall里面挨个BFS遍历,如果从一个'O'出发的所有点都满足封住了,则这BFS走过的点都被封住了,最后赋值为'X'(这样会超时)

2. 而这道题的思路比较好的是逆过来,从边界的'O'出发BFS(因为只要跟边界的'O'连上了,就必然封不住了;而没有与边界的'O'边界连上的'O'自然被封住了)

再说实现细节:

1. 为了不使用额外空间,对于由边界'O'经BFS得到的'O'都置为'M'(M有两个意思:一是访问过了,不用再访问了;二是这个点再最后要恢复为'O')

2. BFS的时候,终止条件第一次直接写的是que.empty(),一直不对;后来改成了!que.empty()才对,这个低级错误不要再犯。

=========================================

第二次过这道题,有个细节没有注意:

如果发现一个点四周的点是要保留的‘O’时,一定要马上将其设置为‘M’,然后再进行BFS;如果不马上设置为‘M',那么在BFS的过程中,可能会重复过这个点。

class Solution {
public:
void solve(vector<vector<char> >& board)
{
if ( board.size()< ) return;
const int m = board.size();
const int n = board[].size();
// search from first row
for ( int i=; i<n; ++i )
{
if ( board[][i]=='O' ) Solution::bfs(board, , i, m, n);
}
// search from last row
for ( int i=; i<n; ++i )
{
if ( board[m-][i]=='O' ) Solution::bfs(board, m-, i, m, n);
}
// search from first column
for ( int i=; i<m; ++i )
{
if ( board[i][]=='O' ) Solution::bfs(board, i, , m, n);
}
// search from last column
for ( int i=; i<m; ++i )
{
if ( board[i][n-]=='O' ) Solution::bfs(board, i, n-, m, n);
}
// trans 'O' to 'X' & trans 'M' to 'O'
for ( int i=; i<m; ++i )
{
for ( int j=; j<n; ++j )
{
if ( board[i][j]=='O' )
{
board[i][j]='X';
continue;
}
if ( board[i][j]=='M' )
{
board[i][j]='O';
}
}
}
}
static void bfs(vector<vector<char> >& board, int r, int c, int ROW, int COL)
{
board[r][c] = 'M';
queue<pair<int, int> > curr;
queue<pair<int, int> > next;
curr.push(make_pair(r, c));
while ( !curr.empty() )
{
while ( !curr.empty() )
{
int i = curr.front().first;
int j = curr.front().second;
curr.pop();
// up
if ( i->= && board[i-][j]=='O' )
{
board[i-][j] = 'M';
next.push(make_pair(i-, j));
}
// down
if ( i+<ROW && board[i+][j]=='O' )
{
board[i+][j] = 'M';
next.push(make_pair(i+, j));
}
// left
if ( j->= && board[i][j-]=='O' )
{
board[i][j-] = 'M';
next.push(make_pair(i, j-));
}
// right
if ( j+<COL && board[i][j+]=='O' )
{
board[i][j+] = 'M';
next.push(make_pair(i, j+));
}
}
swap(next, curr);
}
}
};

【Surrounded Regions】cpp的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  6. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  7. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  8. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

随机推荐

  1. git push 使用教程

    git push命令用于将本地分支的更新,推送到远程主机.它的格式与git pull命令相仿. $ git push <远程主机名> <本地分支名>:<远程分支名> ...

  2. linux中python安装

    1.查看当前环境中是否存在python安装包 [zyj@localhost ~]$ rpm -qa | grep python gnome-python2-gnome--.el6.x86_64 pyt ...

  3. datagrid数据表格使用总结

    一.加载的css文件 easyui 基本样式: <link href="../easyui/easyui1.5.css" rel="stylesheet" ...

  4. Localroast使用总结

    全手打原创,转载请标明出处: https://www.cnblogs.com/dreamsqin/p/10883248.html,多谢~=.= 什么是Localroast 一个根据 JSON 文件快速 ...

  5. SSH中懒加载异常--could not initialize proxy - no Session

    SSH进行关联的表进行显示时出现的问题,老是显示你的OGNL表达式错误,但是找了很久确实没错,在网上找了一下,下面的这个方法本人认为是最有效的方法(已经测试可以使用) 在web.xml中加入 程序代码 ...

  6. 查询MySQL的存储引擎

  7. ABAP和Java的destination和JNDI

    Netweaver里使用事务码SM59创建Destination: Java 新建一个destination: 测试代码: try { Context ctx = new InitialContext ...

  8. POJ-2195 Going Home---KM算法求最小权值匹配(存负边)

    题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格 ...

  9. python_43_移动文件指针补充

    #移动文件指针补充 ''' 文件对象.seek((offset,where)) offset:移动的偏移量,单位为字节.等于正数时向文件尾方向移动,等于负数时向文件头方向移动文件指针 where:指针 ...

  10. x5webview 微信H5支付

    mWebView.setWebViewClient(new WebViewClient() { // @Override // public boolean shouldOverrideUrlLoad ...