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

The year of the tiger is coming. Come and make a wish. I heard that the wish will come true

C nullable type

什么是消息队列?
上海字节面试问题及薪资福利

Modes of optical fiber - single mode and multimode

English grammar_ Noun possessive

The 2022 China low / no code Market Research and model selection evaluation report was released

C#可空类型
![R language [logic control] [mathematical operation]](/img/93/06a306561e3e7cb150d243541cc839.png)
R language [logic control] [mathematical operation]

Digital innovation driven guide
随机推荐
AI人脸编辑让Lena微笑
Mapbox Chinese map address
zabbix_ Get test database failed
5. 数据访问 - EntityFramework集成
Différenciation et introduction des services groupés, distribués et microservices
常用消息队列有哪些?
4. 对象映射 - Mapping.Mapster
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
CVE-2021-3156 漏洞复现笔记
Hcip seventh operation
【日常训练--腾讯精选50】292. Nim 游戏
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
《HarmonyOS实战—入门到开发,浅析原子化服务》
SAP webservice 测试出现404 Not found Service cannot be reached
Taobao Commodity details page API interface, Taobao Commodity List API interface, Taobao Commodity sales API interface, Taobao app details API interface, Taobao details API interface
Zero sequence aperture of leakage relay jolx-gs62 Φ one hundred
如何提高网站权重
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
消息队列:如何确保消息不会丢失
产业金融3.0:“疏通血管”的金融科技