打开这个题,做了一半躺下了。

结果,怎么都睡不着。一会一个想法,忍不住爬起来提交,要么错误,要么超时。

按照常规思路,依次对每个点检测是否是闭包,再替换,超时。计算量太大了。

还能怎么做呢?没思路,关机睡觉!

躺着睡不着了,思考吧。。。闭着眼睛运行代码。。

突然灵机一动,可以反着来啊!

先把非法的干掉,剩下的就是合法的,不再检测,直接替换就可以了。

而且用到了我平时编辑Word常用的方式,先用一个mark来保护不应该替换的!

哈哈  巧妙!

按耐不住,再次爬起来,1分钟敲完代码,提交,AC!激动!

大半夜的做题,我也是疯魔了。。老天看在我这么努力的份上。。让我赶紧开窍吧!

为啥是B,因为代表bad blocks~~

  1. void fill_with_b(char **board, int i, int j, int m, int n)
  2. {
  3. if(i < || i > m- || j < || j > n-) return;
  4. if(board[i][j] == 'O')
  5. {
  6. board[i][j] = 'B';
  7. fill_with_b(board,i+,j,m,n);
  8. fill_with_b(board,i-,j,m,n);
  9. fill_with_b(board,i,j-,m,n);
  10. fill_with_b(board,i,j+,m,n);
  11. }
  12. }
  13. void replace_b(char **board, int m, int n)
  14. {
  15. int i, j;
  16. for(i = ; i < m; i++)
  17. {
  18. for(j = ; j < n; j++)
  19. {
  20. if(board[i][j] == 'B')
  21. {
  22. board[i][j] = 'O';
  23. }
  24. }
  25. }
  26. }
  27. void solve(char** board, int m, int n) {
  28.  
  29. if(board == NULL || m*n ==) return;
  30. if(m < || n < ) return;
  31.  
  32. int i,j;
  33.  
  34. for(i = ; i < m; i++)
  35. {
  36. if(board[i][] == 'O')
  37. fill_with_b(board,i,,m,n);
  38. if(board[i][n-] == 'O')
  39. fill_with_b(board,i,n-,m,n);
  40. }
  41.  
  42. for(j = ; j < n ; j++)
  43. {
  44. if(board[][j] == 'O')
  45. fill_with_b(board,,j,m,n);
  46. if(board[m-][j] == 'O')
  47. fill_with_b(board,m-,j,m,n);
  48. }
  49.  
  50. for(i=;i<m;i++)
  51. {
  52. for(j=;j<n;j++)
  53. {
  54. if(board[i][j] == 'O')
  55. board[i][j] = 'X';
  56. }
  57. }
  58. replace_b(board,m,n);
  59.  
  60. }

130. Surrounded Regions 卧槽!我半梦半醒之间做出来的。的更多相关文章

  1. 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 用了第一种方式, ...

  2. 130. Surrounded Regions(M)

    130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...

  3. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  4. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  5. 130. Surrounded Regions

    题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...

  6. 【一天一道LeetCode】#130. Surrounded Regions

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. 130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  8. Leetcode 130. Surrounded Regions

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  9. 130. Surrounded Regions -- 被某字符包围的区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

随机推荐

  1. windows修改远程桌面端口3389

    regedit 按照路径打开,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-T ...

  2. centos7.0KVM虚拟化

    需要在图形化界面完成实验 查看CPU信息 # cat /proc/cpuinfo centos7默认安装的虚拟化包 # yum list open*tools 如果没安装就安装 #yum instal ...

  3. Shiro Realm

    Realm: 在实际应用中,shiro从数据库中获取安全数据(如用户.角色.权限),而不是从ini中,可作为安全数据源 即SecurityManager要验证用户身份,那么它需要从Realm获取相应的 ...

  4. Spring MVC RESTful

    REST: 即 Representational State Transfer,(资源)表现层状态转化是目前最流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便 具体说,就是 HTTP ...

  5. Select模式和超时

    fd_set rset; FD_ZERO(&rset); int nready; int maxfd; int fd_stdin = fileno(stdin); if(fd_stdin &g ...

  6. Django中的中间件(middleware)

    中间件: 在研究中间件的时候我们首先要知道 1 什么是中间件? 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Djang ...

  7. MySQL多表查询,pymysql模块。

    一 多表查询: 首先什么是多表查询: 我们在实际工作中,不可能把数据都存入一个表中,那么又需要这些表之间有一定的关联,因为表与表之间的数据是相关联的,所以就要用到我们的外键将多表连接到一起,那么我们更 ...

  8. springboot通过poi导出excel

    Maven引入依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi< ...

  9. Redis实现分布式锁原理与实现分析

    一.关于分布式锁 关于分布式锁,可能绝大部分人都会或多或少涉及到. 我举二个例子: 场景一:从前端界面发起一笔支付请求,如果前端没有做防重处理,那么可能在某一个时刻会有二笔一样的单子同时到达系统后台. ...

  10. promise请求数据用法

    Promise简介 Promise 是异步编程的一种解决方案,比传统的解决方案–回调函数和事件--更合理和更强大.ES6将其写进了语言标准,统一了语法,里面保存着某个未来才回结束的事件(通常是一个异步 ...