2014.2.27 00:59
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
Solution:
This problem is a test on Flood Fill Algorithm. You can do it with DFS or BFS, but the latter will result in TLE.
Why is BFS so slow? Because the queue operation is not strictly O(1), but rather ammortized to O(1). The bad case for some of the queue operations will be O(n). While using BFS you don't have to worry about call stack overflow, but rather the speed of a queue.
Total time complexity is O(n^2). Space complexity is O(n^2), mainly from parameters in recursive function calls.
Accepted code:
1 // 1CE, 3TLE, 1AC, DFS accepted but BFS failed? 2 #include <queue> 3 #include <vector> 4 using namespace std; 5 6 class Solution { 7 public: 8 void solve(vector<vector<char> > &board) { 9 // Should n or m be smaller than 3, there'll be no captured region. 10 n = (int)board.size(); 11 if (n < 3) { 12 return; 13 } 14 15 m = (int)board[0].size(); 16 if (m < 3) { 17 return; 18 } 19 20 int i, j; 21 22 // if an 'O' is on the border, all of its connected 'O's are not captured. 23 // so we scan the border and mark those 'O's as free. 24 25 // the top row 26 for (j = 0; j < m; ++j) { 27 if (board[0][j] == 'O') { 28 check_region(board, 0, j); 29 } 30 } 31 32 // the bottom row 33 for (j = 0; j < m; ++j) { 34 if (board[n - 1][j] == 'O') { 35 check_region(board, n - 1, j); 36 } 37 } 38 39 // the left column 40 for (i = 1; i < n - 1; ++i) { 41 if (board[i][0] == 'O') { 42 check_region(board, i, 0); 43 } 44 } 45 46 // the right column 47 for (i = 1; i < n - 1; ++i) { 48 if (board[i][m - 1] == 'O') { 49 check_region(board, i, m - 1); 50 } 51 } 52 53 // other unchecked 'O's are all captured 54 for (i = 0; i < n; ++i) { 55 for (j = 0; j < m; ++j) { 56 if (board[i][j] == '#') { 57 // free 'O's 58 board[i][j] = 'O'; 59 } else if (board[i][j] == 'O') { 60 // captured 'O's 61 board[i][j] = 'X'; 62 } 63 } 64 } 65 } 66 private: 67 int n, m; 68 69 void check_region(vector<vector<char> > &board, int startx, int starty) { 70 if (startx < 0 || startx > n - 1 || starty < 0 || starty > m - 1) { 71 return; 72 } 73 if (board[startx][starty] == 'O') { 74 board[startx][starty] = '#'; 75 check_region(board, startx - 1, starty); 76 check_region(board, startx + 1, starty); 77 check_region(board, startx, starty - 1); 78 check_region(board, startx, starty + 1); 79 } 80 } 81 };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)