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 ...
随机推荐
- [转][html]设置IIS 默认页
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.we ...
- [转][C#]Combobox 行高
namespace System.Windows.Forms { class ComboBoxEx : ComboBox { public ComboBoxEx() { DrawMode = Syst ...
- Django的视图层简介
Django的视图层 视图函数 所谓视图函数,其实就是我们Django项目中各个应用中的views.py文件中定义的每个用来处理URL路由映射到这里的逻辑函数.一个视图函数简称视图,它是个简单的Pyt ...
- [UE4 ]Is Locally Controlled的局限性
一.在有机器人的游戏中,就不能使用IsLocallyControlled判断.而是要使用这个方法: 二.因为机器人也是属于本地控制的角色.所以不能使用IsLocallyControlled判断是否是本 ...
- 00006 - Linux中使用export命令设置环境变量
功能说明:设置或显示环境变量. #################################################################################### ...
- DELPHI中自定义消息的发送和接收
DELPHI中的消息处理机制 Delphi是Borland公司提供的一种全新的WINDOWS编程开发工具.由于它采用了具有弹性的和可重用的面向对象Pascal(object-orientedpasca ...
- C#语言经典例题
两个例题分别用了两种不同的写入方式 一个是有Console.Write(); 一个没有,两种都可以 标准体重 男士体重 = 身高 - 100 +-3 kg cm 女士体重 = 身高 - 110 +-3 ...
- MySQL也真是让人郁闷(关于子查询中使用limit)
在MySQL4.1中子查询是不能使用LIMIT的,手册中也明确指明 “This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY ...
- Java中的Html解析:使用jsoup
包:jsoup-1.10.2.jar import java.io.File; import java.io.IOException; import org.jsoup.Jsoup; import o ...
- ubuntu 16.04 静态ip的配置
nssa-sensor1@nssa-sensor1:~$ vim /etc/network/interfaces 以下是编辑文件的内容# interfaces(5) file used by ifup ...