leetcode -- Surrounded Regions -- 典型题。BFS
2015-12-17 16:13
447 查看
https://leetcode.com/problems/surrounded-regions/
跟https://leetcode.com/problems/number-of-islands/ 一起看,都是这种要找是否搜索过的题目,要把搜索过的点标记起来。通常可以用一个mask matrix来记录是否已经被记录了。
这道题目的意思就是只要是与边上的O连在一起的O就可以不用被flip。所以这里从所有便开始找其上下左右方向的O,然后赋值为D。搜索完之后,再把棋盘中间的O变成X,D变成O就行了
/article/4981573.html
http://yucoding.blogspot.hk/2013/08/leetcode-question-131-surrounded-regions.html 这里用的是很经典的写法。
/article/4981573.html
/article/1337466.html
ref1的code
这里只要是True的话,就不用被flip,值不变。边上的值肯定不变
其实这里可以不用mask matrix bb. 像ref2里面一样直接把board当做记录就行。如果访问过 就置为 ’ D’
跟https://leetcode.com/problems/number-of-islands/ 一起看,都是这种要找是否搜索过的题目,要把搜索过的点标记起来。通常可以用一个mask matrix来记录是否已经被记录了。
这道题目的意思就是只要是与边上的O连在一起的O就可以不用被flip。所以这里从所有便开始找其上下左右方向的O,然后赋值为D。搜索完之后,再把棋盘中间的O变成X,D变成O就行了
思路1 DFS 不能过big case
参考/article/4981573.html
思路2 BFS 可以AC
思路:http://yucoding.blogspot.hk/2013/08/leetcode-question-131-surrounded-regions.html 这里用的是很经典的写法。
/article/4981573.html
/article/1337466.html
ref1的code
这里只要是True的话,就不用被flip,值不变。边上的值肯定不变
class Solution: # @param board, a 2D array # Capture all regions by modifying the input board in-place. # Do not return any value. def solve(self, board): row = len(board) if row==0: return col = len(board[0]) bb = [[False for j in xrange(0,col)] for i in xrange(0,row)] que = [] for i in xrange(0,col): if board[0][i]=='O': bb[0][i]=True que.append([0,i]) if board[row-1][i]=='O': bb[row-1][i]=True que.append([row-1,i]) for i in xrange(0,row): if board[i][0]=='O': bb[i][0]=True que.append([i,0]) if board[i][col-1]=='O': bb[i][col-1]=True que.append([i,col-1]) while que: i = que[0][0] j = que[0][1] que.pop(0) if (i-1>0 and board[i-1][j]=='O' and bb[i-1][j]==False): bb[i-1][j]=True que.append([i-1,j]) if (i+1<row-1 and board[i+1][j]=='O' and bb[i+1][j]==False): bb[i+1][j]=True que.append([i+1,j]) if (j-1>0 and board[i][j-1]=='O' and bb[i][j-1]==False): bb[i][j-1]=True que.append([i,j-1]) if (j+1<col-1 and board[i][j+1]=='O' and bb[i][j+1]==False): bb[i][j+1]=True que.append([i,j+1]) for i in xrange(0,row): for j in xrange(0,col): if board[i][j]=='O' and bb[i][j]==False: board[i][j] = 'X' return
其实这里可以不用mask matrix bb. 像ref2里面一样直接把board当做记录就行。如果访问过 就置为 ’ D’
class Solution(object): def solve(self, board): """ :type board: List[List[str]] :rtype: void Do not return anything, modify board in-place instead. """ row = len(board) if row==0: return col = len(board[0]) que = [] for i in xrange(0,col): if board[0][i]=='O': board[0][i]='D' que.append([0,i]) if board[row-1][i]=='O': board[row-1][i]='D' que.append([row-1,i]) for i in xrange(0,row): if board[i][0]=='O': board[i][0]='D' que.append([i,0]) if board[i][col-1]=='O': board[i][col-1]='D' que.append([i,col-1]) while que: i = que[0][0] j = que[0][1] que.pop(0) if (i-1>0 and board[i-1][j]=='O' and board[i-1][j] != 'D'): board[i-1][j]='D' que.append([i-1,j]) if (i+1<row-1 and board[i+1][j]=='O' and board[i+1][j] != 'D'): board[i+1][j]='D' que.append([i+1,j]) if (j-1>0 and board[i][j-1]=='O' and board[i][j-1] != 'D'): board[i][j-1]='D' que.append([i,j-1]) if (j+1<col-1 and board[i][j+1]=='O' and board[i][j+1] != 'D'): board[i][j+1]='D' que.append([i,j+1]) for i in xrange(0,row): for j in xrange(0,col): if board[i][j]=='O': board[i][j] = 'X' elif board[i][j] == 'D': board[i][j] = 'O' return
相关文章推荐
- 自动化测试小实例
- Gradle学习笔记(二)
- Oracle 11g R2 64位在 win7 64位的安装流程图解
- hdu 4735Little Wish~ lyrical step~ 重复覆盖
- 信息学奥林匹克竞赛-津津的储蓄计划
- gdb 技巧学习
- 数据挖掘:K最近邻(KNN)算法的java实现
- SELECTOR
- shell脚本中导入mysql数据&&执行mysql语句
- Java输入输出流详解
- MathJax在IE中公式不解析
- java 读取ftp文件时特殊字符乱码
- PHPUnit测试私有属性和方法
- 半平面交总结and模板
- android——生成或者下载的图片在相册中找不到
- 問題排查:index was out of range (1)
- python操作excel表格详解(xlrd/xlwt)
- php屏蔽错误消息
- jquery文件上传控件 Uploadify 问题记录
- jQuery formValidator表单验证插件常见问题