博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] Surrounded Regions
阅读量:7237 次
发布时间:2019-06-29

本文共 2781 字,大约阅读时间需要 9 分钟。

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 . 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
>& 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 neighbors22 void update(vector
>& board, queue
>& 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 regions42 void mark(vector
>& board, int r, int c) {43 queue
> 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
cur = toVisit.front();50 toVisit.pop();51 update(board, toVisit, cur.first, cur.second);52 }53 }54 }55 // Capture surrounded regions56 void capture(vector
>& 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 };

 

转载地址:http://tfwbm.baihongyu.com/

你可能感兴趣的文章
说说WordPress的主查询函数-query_posts()
查看>>
认识Linux中的LVM PV VG LV
查看>>
SEO如何写好文章标题
查看>>
剖析产品 找准用户 做个创业“老炮儿” --司马亮创业回忆录(二)
查看>>
How GoldenGate process consumes memory
查看>>
zabbix触发器无法执行动作
查看>>
openstack 之 控制节点物理机备份
查看>>
安装docker-compose的两种方式
查看>>
CentOS7.3编译安装MariaDB10.2.12
查看>>
linux实现cobbler
查看>>
如何在CentOS 7上修改主机名
查看>>
puppet自动化运维之tag标签puppet自动化运维之tag标签
查看>>
常见问题kernel调优
查看>>
通过vftps和虚拟帐号增强ftp的安全性
查看>>
20个最新的照片 PS 技巧,提升摄影水平
查看>>
通过SAN(Subject Alternative Name)实现证书的多域名安全访问
查看>>
RHEL6 搭建 keepalived + lvs/DR 集群
查看>>
easypanel 的 PHP5.4-7.0 插件
查看>>
System Center Operations Manager 2012 SP1 处理“未监控”对象
查看>>
JQuery链式操作学习对比
查看>>