[LeetCode] Surrounded Regions

本文介绍了一种优化BFS算法解决迷宫问题的方法,通过避免不必要的检查和利用边界上的'O'字符进行标记,有效减少了运行时间。文章包含了一些小型优化,如将访问队列传递到更新函数中,同时提供了实现代码。通过这种方式,可以提高算法效率并避免TLE错误。

This problem is not quite difficult (a typical BFS traversal of graph), though, its aceptance rate is relatively low. In fact, the key obstacle in passing this problem is how to reduce the number of checks and avoid the annoying TLE.

There is a nice idea in this link. In fact, all the surrounded regions will not contain an O on the boundary. This idea takes advantage of this observation and visit the board from O's on the boundary and mark them using another character, say, #. Finally, all the remaining O's are the surrounded regions and should be captured to X. The # regions simply need to be recovered to O.

I adopt the idea from the above link. And I include some minor optimizations. For example, I pass the queue toVisit to the update function to check and update the four neighbors together during the BFS. This turns out to reduce the running time from 28ms to 16ms.

The code is as follows.

 1 class Solution {
 2 public:
 3     void solve(vector<vector<char>>& board) {
 4         if (board.empty()) return;
 5         int m = board.size(), n = board[0].size();
 6         for (int i = 0; i < m; i++) {
 7             if (board[i][0] == 'O')
 8                 mark(board, i, 0);
 9             if (board[i][n - 1] == 'O')
10                 mark(board, i, n - 1);
11         }
12         for (int j = 0; j < n; j++) {
13             if (board[0][j] == 'O')
14                 mark(board, 0, j);
15             if (board[m - 1][j] == 'O')
16                 mark(board, m - 1, j);
17         }
18         capture(board);
19     }
20 private:
21     // Update neighbors
22     void update(vector<vector<char>>& board, queue<pair<int, int>>& toVisit, int r, int c) {
23         int m = board.size(), n = board[0].size();
24         if (r - 1 >= 0 && board[r - 1][c] == 'O') {
25             board[r - 1][c] = '#';
26             toVisit.push(make_pair(r - 1, c));
27         }
28         if (r + 1 < m && board[r + 1][c] == 'O') {
29             board[r + 1][c] = '#';
30             toVisit.push(make_pair(r + 1, c));
31         }
32         if (c - 1 >= 0 && board[r][c - 1] == 'O') {
33             board[r][c - 1] = '#';
34             toVisit.push(make_pair(r, c - 1));
35         }
36         if (c + 1 < n && board[r][c + 1] == 'O') {
37             board[r][c + 1] = '#';
38             toVisit.push(make_pair(r, c + 1));
39         }
40     }
41     // Mark non-surrounded regions
42     void mark(vector<vector<char>>& board, int r, int c) {
43         queue<pair<int, int> > toVisit;
44         toVisit.push(make_pair(r, c));
45         board[r][c] = '#';
46         while (!toVisit.empty()) {
47             int num = toVisit.size();
48             for (int i = 0; i < num; i++) {
49                 pair<int, int> cur = toVisit.front();
50                 toVisit.pop();
51                 update(board, toVisit, cur.first, cur.second);
52             }
53         }
54     }
55     // Capture surrounded regions
56     void capture(vector<vector<char>>& board) {
57         int m = board.size(), n = board[0].size();
58         for (int i = 0; i < m; i++) {
59             for (int j = 0; j < n; j++) {
60                 if (board[i][j] == '#')
61                     board[i][j] = 'O';
62                 else board[i][j] = 'X';
63             }
64         }
65     }
66 };

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4598456.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值