【leetcode】980. Unique Paths III
题目如下:
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 <= 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的更多相关文章
- 【LeetCode】980. Unique Paths III解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】63. Unique Paths II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 【LeetCode】63. Unique Paths II
Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to ...
- 【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 ...
- 【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). ...
- 【LeetCode】063. Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【一天一道LeetCode】#63. Unique Paths II
一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...
- 【LeetCode】732. My Calendar III解题报告
[LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...
随机推荐
- Python爬虫之抓取豆瓣影评数据
脚本功能: 1.访问豆瓣最受欢迎影评页面(http://movie.douban.com/review/best/?start=0),抓取所有影评数据中的标题.作者.影片以及影评信息 2.将抓取的信息 ...
- sql判断中文、数字、英文
IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULL DROP FUNCTION DBO.GET_NUMBER2 GO )) ) AS BEGIN BEGIN ,'' ...
- PHP curl_exec函数
curl_exec — 执行一个cURL会话 说明 mixed curl_exec ( resource $ch ) 执行给定的cURL会话. 这个函数应该在初始化一个cURL会话并且全部的选项都被设 ...
- 控制透明度(兼容IE FF)
filter:alpha(opacity=70);opacity:0.7;
- Scribd每月共有超过两亿个访客、累积数亿篇以上的文件档案,Alexa全球排名200以内
目前已登上世界300大网站,每月共有超过两亿个访客.累积数亿篇以上的文件档案.透过Flash介面的阅读器-iPaper,使用者可以在网站内浏览各种文件,由于该网站是一个文件分享平台,所有的文件都是由使 ...
- 建站手册-网站构建:万维网联盟(World Wide Web Consortium)
ylbtech-建站手册-网站构建:万维网联盟(World Wide Web Consortium) 1.返回顶部 1. http://www.w3school.com.cn/site/site_w3 ...
- selenium2-java 浏览器操作常用命令语法
WebDriver driver = new ChromeDriver(); xpath定位:driver.findElement(By.xpath("//div[@id='register ...
- jenkins配置到gitlab拉代码
参照: jenkins 从git拉取代码-简明扼要 https://www.cnblogs.com/jwentest/p/7065783.html 持续集成①安装部署jenkins从git获取代码-超 ...
- 目前写出的bug
要检测p->next 与p都不=NULL while(p->next!=NULL &&p!=NULL) 会导致访问access NULL pointer的runtime错误 ...
- Linux(Ubuntu)常用命令(四)
权限修改: 先知: u user 表示该文件的所有者 g group 表示与该文件的所有者属于同一组( group )者,即用户组 o other 表示其他以外的人 a all 表示这三者皆是 r r ...