当前位置:网站首页>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;
}
}
}
}
}
}边栏推荐
猜你喜欢

English语法_名词 - 所有格

判断文件是否为DICOM文件

Common skills and understanding of SQL optimization
![[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training](/img/d6/e6db0d76e81e49a83a30f8c1832f09.png)
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training

English grammar_ Noun possessive

Flink SQL realizes reading and writing redis and dynamically generates hset key

Bat instruction processing details

如何提高网站权重

Leakage relay jelr-250fg

JVM the truth you need to know
随机推荐
How to get free traffic in pinduoduo new store and what links need to be optimized in order to effectively improve the free traffic in the store
The 2022 China low / no code Market Research and model selection evaluation report was released
Modes of optical fiber - single mode and multimode
关于服装ERP,你知道多少?
原生小程序 之 input切换 text与password类型
拼多多新店如何获取免费流量,需要从哪些环节去优化,才能有效提升店内免费流量
Jhok-zbg2 leakage relay
win配置pm2开机自启node项目
Taobao store release API interface (New), Taobao oauth2.0 store commodity API interface, Taobao commodity release API interface, Taobao commodity launch API interface, a complete set of launch store i
Unity让摄像机一直跟随在玩家后上方
Polynomial locus of order 5
5阶多项式轨迹
Win configuration PM2 boot auto start node project
Simple case of SSM framework
1. AVL tree: left-right rotation -bite
Message queuing: how to ensure that messages are not lost
【Shell】清理nohup.out文件
上海字节面试问题及薪资福利
OpenSergo 即将发布 v1alpha1,丰富全链路异构架构的服务治理能力
English语法_名词 - 所有格