标签:
题目:
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<3 ) return; const int COL = board[0].size(); // first row for ( int i = 0; i < COL; ++i ) {if ( board[0][i]==‘O‘) { board[0][i] = ‘M‘; Solution::bfs(board, 0, i, ROW, COL); }} // last row for ( int i = 0; i < COL; ++i ) {if ( board[ROW-1][i]==‘O‘ ) { board[ROW-1][i] = ‘M‘; Solution::bfs(board, ROW-1, i, ROW, COL); }} // first col for ( int i = 0; i < ROW; ++i ) {if ( board[i][0]==‘O‘ ) { board[i][0] = ‘M‘; Solution::bfs(board, i, 0, ROW, COL); }} // last col for ( int i = 0; i < ROW; ++i ) {if ( board[i][COL-1]==‘O‘ ) { board[i][COL-1]=‘M‘; Solution::bfs(board, i, COL-1, ROW, COL); }} // flipping surrounded regions for ( int i = 0; i < ROW; ++i ){ for ( int j = 0; 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-1>0 && board[r-1][c]==‘O‘ ) { board[r-1][c]=‘M‘; que.push(make_pair(r-1, c)); } if ( c-1>0 && board[r][c-1]==‘O‘) { board[r][c-1]=‘M‘; que.push(make_pair(r, c-1)); } if ( r+1<ROW-1 && board[r+1][c]==‘O‘ ) { board[r+1][c]=‘M‘; que.push(make_pair(r+1, c)); } if ( c+1<COL-1 && board[r][c+1]==‘O‘ ) { board[r][c+1]=‘M‘; que.push(make_pair(r, c+1)); } } } };
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()才对,这个低级错误不要再犯。
标签:
原文地址:http://www.cnblogs.com/xbf9xbf/p/4530100.html