当前位置:网站首页>980. 不同路径 III DFS
980. 不同路径 III DFS
2022-07-07 00:25:00 【钰娘娘】
980. 不同路径 III
在二维网格
grid上,有 4 种类型的方格:
1表示起始方格。且只有一个起始方格。2表示结束方格,且只有一个结束方格。0表示我们可以走过的空方格。-1表示我们无法跨越的障碍。返回在四个方向(上、下、左、右)上行走时,从起始方格到结束方格的不同路径的数目。
每一个无障碍方格都要通过一次,但是一条路径中不能重复通过同一个方格。
示例 1:
输入:[[1,0,0,0],[0,0,0,0],[0,0,2,-1]] 输出:2 解释:我们有以下两条路径: 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)示例 2:
输入:[[1,0,0,0],[0,0,0,0],[0,0,0,2]] 输出:4 解释:我们有以下四条路径: 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)示例 3:
输入:[[0,1],[2,0]] 输出:0 解释: 没有一条路能完全穿过每一个空的方格一次。 请注意,起始和结束方格可以位于网格中的任意位置。提示:
1 <= grid.length * grid[0].length <= 20
做题结果
成功
方法:DFS
1. 找到起点位置,终点位置,空地数
2. 从起点出发依次尝试每条路径,占用过的空地修改为1,尝试之后还原回0
class Solution {
public int uniquePathsIII(int[][] grid) {
int[] start = null;
int[] end = null;
int m = grid.length;
int n = grid[0].length;
int empty = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
switch(grid[i][j]){
case 0: {++empty; break;}
case 1: start = new int[]{i,j}; break;
case 2: end = new int[]{i,j}; break;
}
}
}
dfs(start,end,grid,empty);
return ans;
}
int ans = 0;
int[] directions = new int[]{0,1,0,-1,0};
private void dfs(int[] start, int[] end,int[][]grid,int empty){
int m = grid.length;
int n = grid[0].length;
for(int i = 0; i < 4; i++){
int x = start[0]+directions[i];
int y = start[1]+directions[i+1];
if(x>=0&&x<m&&y>=0&&y<n){
if(empty == 0){
if(x==end[0]&&y==end[1])++ans;
}else{
if(grid[x][y]==0){
grid[x][y]=1;
dfs(new int[]{x,y},end,grid,empty-1);
grid[x][y]=0;
}
}
}
}
}
}边栏推荐
- Tablayout modification of customized tab title does not take effect
- 判断文件是否为DICOM文件
- 随机生成session_id
- Jhok-zbg2 leakage relay
- Unity keeps the camera behind and above the player
- pytorch_ 01 automatic derivation mechanism
- Leakage relay jd1-100
- zabbix_get测试数据库失败
- Leetcode: maximum number of "balloons"
- Modes of optical fiber - single mode and multimode
猜你喜欢

ForkJoin最全详解(从原理设计到使用图解)

Différenciation et introduction des services groupés, distribués et microservices

Cve-2021-3156 vulnerability recurrence notes

Determine whether the file is a DICOM file

三级菜单数据实现,实现嵌套三级菜单数据

nVisual网络可视化

随机生成session_id

得物客服一站式工作台卡顿优化之路

Flink SQL 实现读写redis,并动态生成Hset key

Mapbox Chinese map address
随机推荐
SAP webservice 测试出现404 Not found Service cannot be reached
WEB架构设计过程
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
得物客服一站式工作台卡顿优化之路
分布式事务解决方案之TCC
Mysql-centos7 install MySQL through yum
Educational Codeforces Round 22 B. The Golden Age
原生小程序 之 input切换 text与password类型
【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
JVM the truth you need to know
Web architecture design process
Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]
Dj-zbs2 leakage relay
bat 批示处理详解
2pc of distributed transaction solution
不同网段之间实现GDB远程调试功能
论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
三级菜单数据实现,实现嵌套三级菜单数据
消息队列:消息积压如何处理?
Flink SQL 实现读写redis,并动态生成Hset key