leetcode1030
class Solution(object):
def __init__(self):
self.List = list() def bfs(self,R,C,S,V):
T = list()
while len(S) >0:
node = S.pop(0)
if node[0]-1>=0 and V[node[0]-1][node[1]] == 0:
V[node[0]-1][node[1]] = 1
T.append([node[0]-1,node[1]])
self.List.append([node[0]-1,node[1]])
if node[0]+1<R and V[node[0]+1][node[1]] == 0:
V[node[0]+1][node[1]] = 1
T.append([node[0]+1,node[1]])
self.List.append([node[0]+1,node[1]])
if node[1]-1>=0 and V[node[0]][node[1]-1] == 0:
V[node[0]][node[1]-1] = 1
T.append([node[0],node[1]-1])
self.List.append([node[0],node[1]-1])
if node[1]+1<C and V[node[0]][node[1]+1] == 0:
V[node[0]][node[1]+1] = 1
T.append([node[0],node[1]+1])
self.List.append([node[0],node[1]+1])
if len(T)>0:
self.bfs(R,C,T,V) def allCellsDistOrder(self, R: int, C: int, r0: int, c0: int) -> 'List[List[int]]':
stack = list()
visited = [[0 for col in range(C)] for row in range(R)]
stack.append([r0,c0])
visited[r0][c0] = 1
self.List.append([r0,c0])
self.bfs(R,C,stack,visited)
return self.List
典型的BFS算法,每一“层”都比前一层的距离多1,因此按层遍历的顺序,即为所求。
leetcode1030的更多相关文章
- [Swift]LeetCode1030. 距离顺序排列矩阵单元格 | Matrix Cells in Distance Order
We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 & ...
随机推荐
- 翻译1-在SQL Server 2016中介绍微软R服务
在SQL Server 2016中介绍微软R服务 源自:http://www.sqlservercentral.com/articles/Microsoft/145393/ 作者:tomakatrun ...
- numpy-随机数
import numpy as np nr=np.random nr.seed(0) np.set_printoptions(precision=2) # 只显示小数点后2位 print(nr.ran ...
- python安装pip管理工具
(1)安装python2.7.5,这里我选择安装在C盘根目录下. (2)安装完毕后C盘会生成一个叫“python27”的文件夹. (3)打开python27,会发现该目录下存在一个叫Scripts的文 ...
- linux ipv6开启的配置文件
1./etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0TYPE=EthernetUUID=9d1d6e2a-cfc5-4e60-8f28-b77 ...
- winform获取EXE图片
winform获取EXE图片 using (FileStream fs = new System.IO.FileStream(n, FileMode.OpenOrCreate, FileAccess. ...
- Unity动画机制 Animator与Animator Controller教程
Unity动画机制Animator 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- web.html
在“Web页”节点下,展开WEB-INF节点,然后双击web.xml文件进行查看. web.xml文件包含Facelets应用程序所需的几个元素.使用NetBeans IDE创建应用程序时,将自动创建 ...
- PHP 框架
LARAVEL/LUMEN, CI ,THINKPHP, YII ,SYMFONY YAF, PHALCON ,ICE FRAMEWORK
- 集群相关、用keepalived配置高可用集群
1.集群相关 2.keepalived相关 3.用keepalived配置高可用集群 安装:yum install keepalived -y 高可用,主要是针对于服务器硬件或服务器上的应用服务而 ...
- BIOS + MBR > UEFI + GPT
BIOS + MBR > UEFI + GPT硬件接口系统与磁盘分区UEFI用于取代老旧的BIOS,而GPT则取代老旧的MBR. 名词解释: BIOS (Basic Input/Output S ...