题目如下:

On a 2-dimensional grid, there are 4 types of squares:

  • 1 represents the starting square.  There is exactly one starting square.
  • 2 represents the ending square.  There is exactly one ending square.
  • 0 represents empty squares we can walk over.
  • -1 represents obstacles that we cannot walk over.

Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.

Example 1:

Input: [[1,0,0,0],[0,0,0,0],[0,0,2,-1]]
Output: 2
Explanation: We have the following two paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2)
2. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2)

Example 2:

Input: [[1,0,0,0],[0,0,0,0],[0,0,0,2]]
Output: 4
Explanation: We have the following four paths:
1. (0,0),(0,1),(0,2),(0,3),(1,3),(1,2),(1,1),(1,0),(2,0),(2,1),(2,2),(2,3)
2. (0,0),(0,1),(1,1),(1,0),(2,0),(2,1),(2,2),(1,2),(0,2),(0,3),(1,3),(2,3)
3. (0,0),(1,0),(2,0),(2,1),(2,2),(1,2),(1,1),(0,1),(0,2),(0,3),(1,3),(2,3)
4. (0,0),(1,0),(2,0),(2,1),(1,1),(0,1),(0,2),(0,3),(1,3),(1,2),(2,2),(2,3)

Example 3:

Input: [[0,1],[2,0]]
Output: 0
Explanation:
There is no path that walks over every empty square exactly once.
Note that the starting and ending square can be anywhere in the grid.

Note:

  1. 1 <= grid.length * grid[0].length <= 20

解题思路:因为grid数据非常少,所以直接DFS/BFS即可得到答案。遍历grid的过程中记录每个节点是否已经遍历过,通过记录已经遍历了遍历节点的总数

代码如下:

class Solution(object):
def uniquePathsIII(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
import copy
visit = []
count = 0
total = len(grid) * len(grid[0])
startx,starty = 0,0
for i in range(len(grid)):
visit.append([0] * len(grid[i]))
for j in range(len(grid[i])):
if grid[i][j] == -1:
count += 1
elif grid[i][j] == 1:
startx,starty = i,j
visit[startx][starty] = 1
queue = [(startx,starty,copy.deepcopy(visit),1)]
res = 0
while len(queue) > 0:
x,y,v,c = queue.pop(0)
if grid[x][y] == 2 and c == total - count:
res += 1
continue
direction = [(-1,0),(1,0),(0,1),(0,-1)]
for i,j in direction:
if x + i >= 0 and x + i < len(grid) and y + j >= 0 and y + j < len(grid[0]) and v[x+i][y+j] == 0 and grid[x+i][y+j] != -1:
v_c = copy.deepcopy(v)
v_c[x+i][y+j] = 1
queue.append((x+i,y+j,v_c,c+1))
return res

【leetcode】980. Unique Paths III的更多相关文章

  1. 【LeetCode】980. Unique Paths III解题报告(C++)

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

  2. 【LeetCode】63. Unique Paths II 解题报告(Python & C++)

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

  3. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

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

  4. 【LeetCode】63. Unique Paths II

    Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...

  5. 【LeetCode】62. Unique Paths

    Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...

  6. 【LeetCode】062. Unique Paths

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  7. 【LeetCode】063. Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  8. 【一天一道LeetCode】#63. Unique Paths II

    一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...

  9. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

随机推荐

  1. 【leetcode】726. Number of Atoms

    题目如下: 解题思路:我用的是递归的方法,每次找出与第一个')'匹配的'('计算atom的数量后去除括号,只到分子式中没有括号为止.例如 "K4(ON(SO3)2)2" -> ...

  2. 【RabbitMQ】Concurrency、Prefetch、exclusive

    分布式消息中间件 RabbitMQ是用Erlang语言编写的分布式消息中间件,常常用在大型网站中作为消息队列来使用,主要目的是各个子系统之间的解耦和异步处理.消息中间件的基本模型是典型的生产者-消费者 ...

  3. objc_setAssociatedObject 关联对象

    使用场景:在分类中,不允许创建实例变量,这里就解决了此问题 参考: https://www.cnblogs.com/someonelikeyou/p/7162613.html 属性的实质:就是实例变量 ...

  4. AT2705 Yes or No(组合数学)

    传送门 解题思路 首先将这个模型放到坐标轴上,\(x\)轴表示\(1\),\(y\)轴表示\(0\).问题就转化成了从\((0,0)\)走到\((n,m)\),每次可以猜测向\(x\)轴或向\(y\) ...

  5. vue仿追书神器,vue小说项目源码

    vue-reader 一点阅读器!API源自追书神器,免费使用!目前已初步开发完成! Github项目地址:https://github.com/AntonySufer/vue-readle 欢迎is ...

  6. 认识setFactory

    平常设置或者获取一个View时,用的较多的是setContentView或LayoutInflater#inflate,setContentView内部也是通过调用LayoutInflater#inf ...

  7. (转)使用InfluxDB+cAdvisor+Grafana配置Docker监控

    文档来源 文档来源:How to setup Docker Monitoring 由garyond翻译.校正及整理 Docker监控简介 我们提供的Docker主机和容器越来越多,对Docker服务器 ...

  8. 因果卷积(causal)与扩展卷积(dilated)

    因果卷积(causal)与扩展卷积(dilated)之An Empirical Evaluation of Generic Convolutional and Recurrent Networks f ...

  9. (二)linux内核准备及编译

    1. 内核下载地址 linux内核网站,可以拿到最新的和最近的稳定版本内核: https://www.kernel.org/ 通过网站下载压缩包后解压或者使用git下载到本地: git clone h ...

  10. 用shell脚本实现MongoDB数据库自动备份

    一.创建MongoDB备份目录 用来存放数据 mkdir -p /data/mongodb_bak/mongodb_bak_now mkdir -p /data/mongodb_bak/mongodb ...