当前位置:网站首页>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;
}
}
}
}
}
}
边栏推荐
- Common skills and understanding of SQL optimization
- Web Authentication API兼容版本信息
- 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
- MySQL-CentOS7通过YUM安装MySQL
- 京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口
- Digital innovation driven guide
- Mysql-centos7 install MySQL through yum
- sql优化常用技巧及理解
- STM32按键状态机2——状态简化与增加长按功能
- R language [logic control] [mathematical operation]
猜你喜欢
消息队列:消息积压如何处理?
论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】
力扣102题:二叉树的层序遍历
判断文件是否为DICOM文件
Harmonyos practice - Introduction to development, analysis of atomized services
Dj-zbs2 leakage relay
Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
Jhok-zbg2 leakage relay
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training
High voltage leakage relay bld-20
随机推荐
消息队列:如何确保消息不会丢失
Jhok-zbl1 leakage relay
Mapbox Chinese map address
Educational Codeforces Round 22 B. The Golden Age
async / await
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
关于服装ERP,你知道多少?
Jhok-zbg2 leakage relay
【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
[shell] clean up nohup Out file
4. Object mapping Mapster
1. AVL tree: left-right rotation -bite
【Shell】清理nohup.out文件
不同网段之间实现GDB远程调试功能
Five core elements of architecture design
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
zabbix_ Get test database failed
SQL query: subtract the previous row from the next row and make corresponding calculations
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
Leakage relay jelr-250fg