题目:

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. VS功能扩展--扩展介绍

    使用Eclipse的朋友都知道Eclipse是一个完全可扩展的IDE,那么在windows程序开发时,我们常使用的IDE(Visual studio)是否具有功能的扩展性呢?毫无疑问,回答是肯定的.我 ...

  2. 报错:Program "sh" not found in PATH

    参考原文:http://vin-mail.blog.163.com/blog/static/37895280201211932919513/ 如果你按照我的方法 先配置了cygwin的环境变量,在打开 ...

  3. nmap -sT -A --script=smb-check-vulns -PO 172.16.21.170

    nmap -sT -A --script=smb-check-vulns -PO 172.16.21.170 调用了nmap的插件--script=smb-check-vulns -sT 隐蔽的tcp ...

  4. 一、基于Qt的图像矩形区域改色

    Qt环境下图像的打开和涂色 一.设计目标 能够在 Qt QtCreator 环境下打开常用图像格式文件,诸如 bmp.jpg.png 图像等,然后将他们转化为 Qt 中的 QImage 类,并进行矩形 ...

  5. JavaScript_HTML DEMO_1_概念

    HTML DOM - 文档对象模型 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). 1. 通过可编程的对象模型,JavaScript获得了足够的能力来创 ...

  6. window7防火墙无法更改某些设置,错误代码0×80070422

    原因:这是由于管理工具的服务中的windows防火墙被禁用了. 解决方案:在window7中点击控制面板,然后点击管理工具,在点服务,然后找到windows firewall 然后将其改为自动就就可以 ...

  7. 未能加载文件或程序集“System.Web.Http, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)解决办法

    1.查看引用处是否确实引用, 2.查看<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1& ...

  8. 【洛谷1120】小木棍(一道有技巧的dfs)

    点此看题面 大致题意: 给你\(N\)根小木棍,请你把它们拼成若干根长度相同的木棍,问你最小可能长度. 枚举+\(dfs\) 显然的,木棍的长度肯定是\(\sum_{i=1}^n len[i]\)的一 ...

  9. numpy.random.shuffle(x)的用法

    numpy.random.shuffle(x) Modify a sequence in-place by shuffling its contents. Parameters: x : array_ ...

  10. 《坐热板凳》第九次团队作业:Beta冲刺与验收准备(第一天)

    <坐热板凳>第九次团队作业:Beta冲刺与验收准备 项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https ...