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 ...
随机推荐
- onunload事件火狐不支持,在IE浏览器中,只有刷新时该事件才发生
onunload事件火狐不支持,在IE浏览器中,只有刷新时该事件才发生
- HTML-Ronoob-基础教程:HTML 字符实体
ylbtech-HTML-Ronoob-基础教程:HTML 字符实体 1.返回顶部 1. HTML 中的预留字符必须被替换为字符实体. HTML 实体 在 HTML 中,某些字符是预留的. 在 HTM ...
- Hive学习笔记一
1. Load的使用 //在1.x版本中定义long数据类型会报错(用bigint代替) create table t_load_stu(name string,age bigint) row for ...
- 1121 Damn Single (25 分)
1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who is bei ...
- go语言学习--指针数组和数组指针
数组指针(也称行指针)定义 int (*p)[n];()优先级高,首先说明p是一个指针,指向一个整型的一维数组,这个一维数组的长度是n,也可以说是p的步长.也就是说执行p+1时,p要跨过n个整型数据的 ...
- awk 改名
awk 改名 echo ""|awk 'END{for(i=2;i<=100;i++){system("mv securitycode\ \("i&quo ...
- [UE4]小技巧:自动添加函数返回值
将一个变量拖放到返回节点上面会自动创建响应类型的返回值 同样的,函数参数也可以这样来做:
- tpadmin导入数据库问题
tpadmin导入数据库后,修改表名tp_web_log_all时报1168错误 原因:该为联合表,原始sql语句中的联合表tp_web_log_001未改变表名,必须修改后用sql语句添加该表
- Mysql之 配置文件读取顺序
On Unix, Linux and Mac OS X, MySQL programs read startup options from the following files, in the sp ...
- Frobenius 范数