DFS - 980. Unique Paths III
时间:2022-07-25
本文章向大家介绍DFS - 980. Unique Paths III,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
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)
思路:
这是求不同路径的第三个变形题,与62和63题不同的是,对每个方格又增加了状态,1代表起点,2代表终点,0代表可以走,-1代表不可以走,最容易想到的就是使用dfs来求解,往四个方向探索回溯就可以。
代码:
go :
func uniquePathsIII(grid [][]int) int {
sx := -1
sy := -1
n := 1 // 起点算1
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[0]); j++ {
if grid[i][j] == 0 {
n++
} else if grid[i][j] == 1 {
sx = i
sy = j
}
}
}
return dfs(grid, sx, sy, n)
}
var dics = [][]int{{1, 0},{-1, 0},{0, 1},{0, -1}}
func dfs(grid [][]int, x, y int, n int) int {
if x < 0 || x == len(grid) || y < 0 || y == len(grid[0]) || grid[x][y] == -1 {
return 0
}
// ending
if grid[x][y] == 2 {
if n == 0 {
return 1
}
return 0
}
var paths int
grid[x][y] = -1
for _, dic := range dics {
paths += dfs(grid, x + dic[0], y + dic[1], n -1)
}
grid[x][y] = 0
return paths
}
- 协议森林16 小美的桌号(DHCP协议)
- struts2(一)之初识struts2
- AI聊天机器人备受青睐 专家呼吁少卖萌
- CQRS框架:AxonFramework 之 Hello World
- Kubernetes的服务网格(第4部分):通过流量切换持续部署
- TortoiseGit安装与配置
- mongodb 速成笔记
- oAuth 2.0 笔记
- JavaScript内置对象--Math对象
- oAuth 2.0 笔记
- common-pool2 学习:thrift连接池的另一种实现
- IntelliJ IDEA 17和Maven构建javaWeb项目
- spring-boot 速成(2) devtools之热部署及LiveReload
- spring-boot 速成(2) devtools之热部署及LiveReload
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 在GitLab pages上快速搭建Jekyll博客
- Dubbo项目中No provider available for the service xxx from registry xxx on the consumer问题的解决思路
- Mysql面对高并发修改的问题处理【2】
- java (多网卡环境下)发送组播广播(multicast/broadcast)失败问题
- activmq:android平台下使用openwire协议连接activemq服务的问题
- Leetcode No.9 回文数
- go-zero微服务框架入门教程
- 聊聊java中的哪些Map:(九)TreeMap源码分析
- 海康IPCamera结合OpenCV图像处理的一般步骤
- 聊聊java中的哪些Map:(十)各种map的总结
- SwiftUI:辅助功能——项目优化示例
- Visualizing the impact of ordered vs. random index insertion in InnoDB (16 顺序插入和随机插入索引的影响可视化分析)
- 一点思考|工作十几年了,竟从未用过do-while!
- springboot 默认日志配置源码
- logback 通过javaconfig实现配置