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 ...
随机推荐
- jQuery的遍历
jQuery有众多的方法,但是有些方法过于累赘,所以就精简了一些重要的方法,记住这些方法的(名字,功能,参数) jQuery的遍历 1.add() 将元素添加到集合中 2.children() 返回被 ...
- HTTPConnectionPool(host:XX)Max retries exceeded with url 解决方法
爬虫多次访问同一个网站一段时间后会出现错误 HTTPConnectionPool(host:XX)Max retries exceeded with url '<requests.package ...
- 对象生命周期中至少被GC一次后存活
Finalize调用流程:GC时,当对象变成(GC Roots)不可达时,若该对象覆盖(重写)了finalize方法并且未执行过finalze方法,则将其放入F-Queue队列,由一低优先级线程执行该 ...
- DNS解析分析
一.基本概念 域名系统: 域名系统(英文:Domain Name System,缩写:DNS)是因特网的一项服务.它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网.DNS使 ...
- MVC 访问静态页面 View 下面放JS
http://blog.csdn.net/qq_17255515/article/details/53293120
- 13-001 ViewComponents IViewComponentActivator
接口定义: /// <summary> /// Provides methods to activate an instantiated ViewComponent /// </su ...
- BCGcontrolBar(三) 添加表格(Grid)组件
表格组件和图表组件是BCG的亮点之一 如下图 BCG有众多的表格样式可供选择 下图是插入基本表格组件后的效果 首先在程序中添加 BasicGridCtrl.h BasicGridCtrl.cpp 因为 ...
- ajax的跨域解决方案(java+ajax)
简单的建立一个后台项目 新建servlet: 内容如下: package a; import java.io.IOException; import java.io.PrintWriter; impo ...
- USACO 2008 Running(贝茜的晨练)
[题解] 动态规划,dp[i][j]表示第i分钟疲劳度为j的最长距离. [代码] #include <iostream> #include <cstdlib> #include ...
- 一个windows计划任务的Nginx日志自动截断的批处理命令
net stop nginx taskkill /im nginx.exe /f cd E:\nginx e: set NO=%Date:~0,4%%Date:~5,2%%Date:~8,2% set ...