当前位置:网站首页>980. Different path III DFS
980. Different path III DFS
2022-07-07 05:53:00 【Empress Yu】
980. Different paths III
On a two-dimensional grid
gridOn , Yes 4 Two types of squares :
1Represents the starting square . And there's only one starting square .2Indicates the end square , And there's only one end square .0An empty square that we can walk through .-1It's an obstacle we can't cross .Return in four directions ( On 、 Next 、 Left 、 Right ) When you go up , The number of different paths from the start square to the end square .
Every barrier free square has to pass once , But you can't go through the same grid repeatedly in a path .
Example 1:
Input :[[1,0,0,0],[0,0,0,0],[0,0,2,-1]] Output :2 explain : We have 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 explain : We have 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)Example 3:
Input :[[0,1],[2,0]] Output :0 explain : There is no way to go through every empty square completely once . Please note that , The start and end squares can be located anywhere in the grid .Tips :
1 <= grid.length * grid[0].length <= 20
The result of doing the question
success
Method :DFS
1. Find the starting point , Final position , Number of open spaces
2. Start from the starting point and try each path in turn , The occupied space is modified to 1, Try to revert back to 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;
}
}
}
}
}
}边栏推荐
- Realize GDB remote debugging function between different network segments
- 数据中心为什么需要一套基础设施可视化管理系统
- Question 102: sequence traversal of binary tree
- 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
- 常用消息队列有哪些?
- AI人脸编辑让Lena微笑
- R语言【逻辑控制】【数学运算】
- yarn入门(一篇就够了)
- ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略
- "Multimodal" concept
猜你喜欢
![[reading of the paper] a multi branch hybrid transformer network for channel terminal cell segmentation](/img/f6/cd307c03ea723e1fb6a0011b37d3ef.png)
[reading of the paper] a multi branch hybrid transformer network for channel terminal cell segmentation

Question 102: sequence traversal of binary tree

404 not found service cannot be reached in SAP WebService test

AI face editor makes Lena smile

Distributed global ID generation scheme

数据中心为什么需要一套基础设施可视化管理系统

What is message queuing?
![[云原生]微服务架构是什么?](/img/84/a0ec68646083f3539aa39ad9d98749.png)
[云原生]微服务架构是什么?

Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface

往图片添加椒盐噪声或高斯噪声
随机推荐
AI face editor makes Lena smile
Randomly generate session_ id
判断文件是否为DICOM文件
What is make makefile cmake qmake and what is the difference?
Paper reading [semantic tag enlarged xlnv model for video captioning]
STM32 key state machine 2 - state simplification and long press function addition
JVM the truth you need to know
【Shell】清理nohup.out文件
数据中心为什么需要一套基础设施可视化管理系统
What EDA companies are there in China?
async / await
SAP ABAP BDC (batch data communication) -018
Message queue: how to handle repeated messages?
404 not found service cannot be reached in SAP WebService test
Sidecar mode
zabbix_get测试数据库失败
Hcip eighth operation
Harmonyos practice - Introduction to development, analysis of atomized services
Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
拼多多新店如何获取免费流量,需要从哪些环节去优化,才能有效提升店内免费流量