题目如下:

We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.

Additionally, we are given a cell in that matrix with coordinates (r0, c0).

Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from smallest distance to largest distance.  Here, the distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance, |r1 - r2| + |c1 - c2|.  (You may return the answer in any order that satisfies this condition.)

Example 1:

Input: R = 1, C = 2, r0 = 0, c0 = 0
Output: [[0,0],[0,1]]
Explanation: The distances from (r0, c0) to other cells are: [0,1]

Example 2:

Input: R = 2, C = 2, r0 = 0, c0 = 1
Output: [[0,1],[0,0],[1,1],[1,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2]
The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.

Example 3:

Input: R = 2, C = 3, r0 = 1, c0 = 2
Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3]
There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].

Note:

  1. 1 <= R <= 100
  2. 1 <= C <= 100
  3. 0 <= r0 < R
  4. 0 <= c0 < C

解题思路:BFS,就这样。

代码如下:

class Solution(object):
def allCellsDistOrder(self, R, C, r0, c0):
"""
:type R: int
:type C: int
:type r0: int
:type c0: int
:rtype: List[List[int]]
"""
visit = []
for i in range(R):
visit.append([0] * C)
direction = [(1,0),(-1,0),(0,1),(0,-1)]
queue = [(r0,c0)]
res = []
while len(queue) > 0:
x,y = queue.pop(0)
if visit[x][y] == 1:
continue
res.append([x, y])
visit[x][y] = 1
for (i,j) in direction:
if x + i >= 0 and x + i < R and y + j >=0 and y+j < C and visit[x+i][y+j] == 0:
queue.append((x+i,y+j))
return res

【leetcode】1030. Matrix Cells in Distance Order的更多相关文章

  1. 【LeetCode】1030. Matrix Cells in Distance Order 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  2. 【Leetcode_easy】1030. Matrix Cells in Distance Order

    problem 1030. Matrix Cells in Distance Order 参考 1. Leetcode_easy_1030. Matrix Cells in Distance Orde ...

  3. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  4. Matrix Cells in Distance Order

    Matrix Cells in Distance Order We are given a matrix with R rows and C columns has cells with intege ...

  5. LeetCode.1030-曼哈顿距离排序矩阵单元格(Matrix Cells in Distance Order)

    这是小川的第384次更新,第412篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第246题(顺位题号是1030).我们给出一个矩阵,其中R行和C列具有整数坐标(r,c)的 ...

  6. 【LeetCode】Set Matrix Zeroes 解题报告

    今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...

  7. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

  8. 【LeetCode】957. Prison Cells After N Days 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 周期是14 日期 题目地址:https://leet ...

  9. 【LeetCode】358. Rearrange String k Distance Apart 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/rearrang ...

随机推荐

  1. docker-compose部署kafka

    docker-compose.yml version: '2' services: zookeeper: image: develop-harbor.geostar.com.cn/3rd/zookee ...

  2. 自定义控件 - 字母索引 : LetterIndexView

    实现字母列表,滑动列表显示当前选中字母,回调接口. 1.实现字母列表.初始化相关属性.计算每个字母所占宽高.绘制字母A-Z,#. private int itemWidth;//每个字母所占宽度 pr ...

  3. 【转】一个 Vim 重度用户总结的 vim 超全指南

    [转]一个 Vim 重度用户总结的 vim 超全指南 我本人是 Vim 的重度使用者,就因为喜欢上这种双手不离键盘就可以操控一切的feel,Vim 可以让我对文本的操作更加精准.高效. 对于未使用过 ...

  4. PHP模拟请求和操作响应

    模拟请求 fsockopen <?php // 建立连接 $link = fsockopen('localhost', '80'); define('CRLF', "\r\n" ...

  5. MySQL-default设置

    Both statements insert a value into the phone column, but the first inserts a NULL value and the sec ...

  6. javascript 动态修改css样式方法汇总(四种方法)

    在很多情况下,都需要对网页上元素的样式进行动态的修改.在JavaScript中提供几种方式动态的修改样式,下面将介绍方法的使用.效果.以及缺陷. 1.使用obj.className来修改样式表的类名. ...

  7. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第6节 static静态_11_静态static关键字概述

    static静态关键字的基本含义 红色教室都划掉 一份数据分给三个人用,而不是三个人相同的数据,三个对象写三遍没有必要. 只写一遍教室,写在学生的类当中.这样还就可以省内存.如果要换教室的话 ,只需要 ...

  8. 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_04 数据结构_2_数据结构_队列

    先进先出 队列 队列:queue,简称队,它同堆栈一样,也是一种运算受限的线性表,其限制是仅允许在表的一端进行插入, 而在表的另一端进行删除. 简单的说,采用该结构的集合,对元素的存取有如下的特点: ...

  9. 接口自动化之提取响应结果(raw、json)

    做接口测试时,我们一般会取得响应结果的部分值,来与预期结果对比,做断言 目录 1.响应结果类型 2.json响应结果的提取 3.raw响应结果的提取 1.响应结果类型 通过fiddler的respon ...

  10. 网络摄像头CVE

    CVE-2018-9995 rtsp未授权访问 rtsp后缀整理: Axis(安讯士) rtsp:// 192.168.200.202/axis-media/media.amp?videocodec= ...