130. Surrounded Regions 卧槽!我半梦半醒之间做出来的。
打开这个题,做了一半躺下了。
结果,怎么都睡不着。一会一个想法,忍不住爬起来提交,要么错误,要么超时。
按照常规思路,依次对每个点检测是否是闭包,再替换,超时。计算量太大了。
还能怎么做呢?没思路,关机睡觉!
躺着睡不着了,思考吧。。。闭着眼睛运行代码。。
突然灵机一动,可以反着来啊!
先把非法的干掉,剩下的就是合法的,不再检测,直接替换就可以了。
而且用到了我平时编辑Word常用的方式,先用一个mark来保护不应该替换的!
哈哈 巧妙!
按耐不住,再次爬起来,1分钟敲完代码,提交,AC!激动!
大半夜的做题,我也是疯魔了。。老天看在我这么努力的份上。。让我赶紧开窍吧!
为啥是B,因为代表bad blocks~~
- void fill_with_b(char **board, int i, int j, int m, int n)
- {
- if(i < || i > m- || j < || j > n-) return;
- if(board[i][j] == 'O')
- {
- board[i][j] = 'B';
- fill_with_b(board,i+,j,m,n);
- fill_with_b(board,i-,j,m,n);
- fill_with_b(board,i,j-,m,n);
- fill_with_b(board,i,j+,m,n);
- }
- }
- void replace_b(char **board, int m, int n)
- {
- int i, j;
- for(i = ; i < m; i++)
- {
- for(j = ; j < n; j++)
- {
- if(board[i][j] == 'B')
- {
- board[i][j] = 'O';
- }
- }
- }
- }
- void solve(char** board, int m, int n) {
- if(board == NULL || m*n ==) return;
- if(m < || n < ) return;
- int i,j;
- for(i = ; i < m; i++)
- {
- if(board[i][] == 'O')
- fill_with_b(board,i,,m,n);
- if(board[i][n-] == 'O')
- fill_with_b(board,i,n-,m,n);
- }
- for(j = ; j < n ; j++)
- {
- if(board[][j] == 'O')
- fill_with_b(board,,j,m,n);
- if(board[m-][j] == 'O')
- fill_with_b(board,m-,j,m,n);
- }
- for(i=;i<m;i++)
- {
- for(j=;j<n;j++)
- {
- if(board[i][j] == 'O')
- board[i][j] = 'X';
- }
- }
- replace_b(board,m,n);
- }
130. Surrounded Regions 卧槽!我半梦半醒之间做出来的。的更多相关文章
- leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 130. Surrounded Regions
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
- 【一天一道LeetCode】#130. Surrounded Regions
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- Leetcode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 130. Surrounded Regions -- 被某字符包围的区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
随机推荐
- windows修改远程桌面端口3389
regedit 按照路径打开,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-T ...
- centos7.0KVM虚拟化
需要在图形化界面完成实验 查看CPU信息 # cat /proc/cpuinfo centos7默认安装的虚拟化包 # yum list open*tools 如果没安装就安装 #yum instal ...
- Shiro Realm
Realm: 在实际应用中,shiro从数据库中获取安全数据(如用户.角色.权限),而不是从ini中,可作为安全数据源 即SecurityManager要验证用户身份,那么它需要从Realm获取相应的 ...
- Spring MVC RESTful
REST: 即 Representational State Transfer,(资源)表现层状态转化是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便 具体说,就是 HTTP ...
- Select模式和超时
fd_set rset; FD_ZERO(&rset); int nready; int maxfd; int fd_stdin = fileno(stdin); if(fd_stdin &g ...
- Django中的中间件(middleware)
中间件: 在研究中间件的时候我们首先要知道 1 什么是中间件? 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Djang ...
- MySQL多表查询,pymysql模块。
一 多表查询: 首先什么是多表查询: 我们在实际工作中,不可能把数据都存入一个表中,那么又需要这些表之间有一定的关联,因为表与表之间的数据是相关联的,所以就要用到我们的外键将多表连接到一起,那么我们更 ...
- springboot通过poi导出excel
Maven引入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi< ...
- Redis实现分布式锁原理与实现分析
一.关于分布式锁 关于分布式锁,可能绝大部分人都会或多或少涉及到. 我举二个例子: 场景一:从前端界面发起一笔支付请求,如果前端没有做防重处理,那么可能在某一个时刻会有二笔一样的单子同时到达系统后台. ...
- promise请求数据用法
Promise简介 Promise 是异步编程的一种解决方案,比传统的解决方案–回调函数和事件--更合理和更强大.ES6将其写进了语言标准,统一了语法,里面保存着某个未来才回结束的事件(通常是一个异步 ...