题目如下:

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. 神经网络学习笔记(二):feedforward和feedback

    维基百科解释: Feed-forward, sometimes written feedforward, is a term describing an element or pathway with ...

  2. 测试md代码折叠功能

    展开查看 System.out.println("Hello to see U!");

  3. Oracle 包的学习

    (1)包是一种数据库对象,相当于一个容器.将逻辑上相关的过程.函数.变量.常量和游标组合成一个更大的单位.用户可以从其他 PL/SQL 块中对其进行引用 (2)包类似于C++和JAVA语言中的类,其中 ...

  4. 处理警告:编码 GBK 的不可映射字符

    怎么处理警告:编码 GBK 的不可映射字符:javac -encoding UTF-8 XX.java使用-encoding参数指明编码方式: 或者 用记事本打开文件,然后另存为,选择ANSI编码,覆 ...

  5. 2019 牛客暑期多校 第八场 A All-one Matrices (单调栈+前缀和)

    题目:https://ac.nowcoder.com/acm/contest/888/A 题意:找全1矩阵的个数,并且这个全1矩阵不被其他全1矩阵包含 思路:这里引用付队说的话 -> { 这类问 ...

  6. TCP/IP协议 和 如何实现 互联网上点对点的通信

    1.参考:https://www.cnblogs.com/onepixel/p/7092302.html   TCP/IP 协议采用4层结构,分别是应用层.传输层.网络层 和 链路层   http 属 ...

  7. error C2872: ‘ofstream’ : ambiguous symbol

    转自VC错误:http://www.vcerror.com/?p=1123 问题描述: 编译时出现: error C2872: 'ofstream' : ambiguous symbol error ...

  8. mysql捕捉所有SQL语句

    MySQL可以通过开通general_log参数(可动态修改)来扑捉所有在数据库执行的SQL语句.显示参数:mysql> show variables like 'general%log%';+ ...

  9. 解决 ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'

    原文链接:https://blog.csdn.net/sea_snow/article/details/82498791 感谢原作者大大  提示:ERROR 1044 (42000): Access ...

  10. CF1241 D Sequence Sorting(离散化+DP)

    题意: 给定数组a[n],用两种操作: 1.将数组中所有值为x的数移至开头 2.将数组中所有值为x的数移至末尾 问,经过最少多少次操作,能将数组a[n]变为非递减的有序数列? (1<=n< ...