在二维网格 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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/unique-paths-iii

很好的一道题,典型的状压DP加DFS,有难度,看了官方做法,佩服,我太菜了。

 class Solution {
int ans;
int[][] grid;
int R, C;
int tr, tc, target;
int[] dr = new int[]{0, -1, 0, 1};
int[] dc = new int[]{1, 0, -1, 0};
Integer[][][] memo; public int uniquePathsIII(int[][] grid) {
this.grid = grid;
R = grid.length;
C = grid[0].length;
target = 0; int sr = 0, sc = 0;
for (int r = 0; r < R; ++r)
for (int c = 0; c < C; ++c) {
if (grid[r][c] % 2 == 0)
target |= code(r, c); if (grid[r][c] == 1) {
sr = r;
sc = c;
} else if (grid[r][c] == 2) {
tr = r;
tc = c;
}
} memo = new Integer[R][C][1 << R*C];
return dp(sr, sc, target);
} public int code(int r, int c) {
return 1 << (r * C + c);
} public Integer dp(int r, int c, int todo) {
if (memo[r][c][todo] != null)
return memo[r][c][todo]; if (r == tr && c == tc) {
return todo == 0 ? 1 : 0;
} int ans = 0;
for (int k = 0; k < 4; ++k) {
int nr = r + dr[k];
int nc = c + dc[k];
if (0 <= nr && nr < R && 0 <= nc && nc < C) {
if ((todo & code(nr, nc)) != 0)
ans += dp(nr, nc, todo ^ code(nr, nc));
}
}
memo[r][c][todo] = ans;
return ans;
}
}

再一次仅记录(我菜呀!)

leetcode #980 不同路径||| (java)的更多相关文章

  1. Leetcode 980. 不同路径 III

    980. 不同路径 III  显示英文描述 我的提交返回竞赛   用户通过次数42 用户尝试次数43 通过次数46 提交次数60 题目难度Hard 在二维网格 grid 上,有 4 种类型的方格: 1 ...

  2. Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)

    Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...

  3. LeetCode:简化路径【71】

    LeetCode:简化路径[71] 题解参考天码营:https://www.tianmaying.com/tutorial/LC71 题目描述 给定一个文档 (Unix-style) 的完全路径,请进 ...

  4. Leetcode 063 不同路径二

    一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为" ...

  5. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  6. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  8. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  9. Atitit,通过pid获取进程文件路径 java php  c#.net版本大总结

    Atitit,通过pid获取进程文件路径 java php  c#.net版本大总结 1. 通过PID获取进程路径的几种方法2 1.1. GetModuleFileNameEx 想获得进程可执行文件的 ...

随机推荐

  1. AI人工智能之基于OpenCV+face_recognition实现人脸识别

    因近期公司项目需求,需要从监控视频里识别出人脸信息.OpenCV非常庞大,其中官方提供的人脸模型分类器也可以满足基本的人脸识别,当然我们也可以训练自己的人脸模型数据,但是从精确度和专业程度上讲Open ...

  2. Visual Studio调试Tersseract

    在Visual Studio中打开Tesseract项目,然后项目-->属性-->调试-->命令变量: 比如在debug目录下有一张 test.jpg的图片,里面有中文需要识别,命令 ...

  3. Move-to-front(MTF) and Run-lenght encoding(RLE) algorithms

    mtf算法(关于该算法:https://www2.cs.duke.edu/csed/algoprobs/beta/bw1.html): #include <stdio.h> #includ ...

  4. Centos610 Oracle 监听文件配置参考

    lister.ora配置参考 # listener.ora Network Configuration File: /home/oracle/app/oracle/product//dbhome_1/ ...

  5. 07-Docker-Image深入理解

    目录 07-Docker-Image深入理解 参考 镜像简介 什么是Docker镜像 什么是Docker容器 镜像结构 镜像特性 镜像层 容器层 镜像存储 07-Docker-Image深入理解 Do ...

  6. 14 用DFT计算线性卷积

    用DFT计算线性卷积 两有限长序列之间的卷积 我们知道,两有限长序列之间的卷积可以用圆周卷积代替,假设两有限长序列的长度分别为\(M\)和\(N\),那么卷积后的长度为\(L=M+N-1\),那么用 ...

  7. 记录我对'我们有成熟的时间复杂度为O(n)的算法得到数组中任意第k大的数'的误解

    这篇博客记录我对剑指offer第2版"面试题39:数组中出现次数超过一半的数字"题解1的一句话的一个小误解,以及汇总一下涉及partition算法的相关题目. 在剑指offer第2 ...

  8. /var/lib/gems/2.5.0/gems/seccomp-tools-1.3.0/lib/seccomp-tools/dumper.rb:125: warning: Insecure world writable dir /home/python/.local in PATH, mode 040777 解决方案

    /var/lib/gems/2.5.0/gems/seccomp-tools-1.3.0/lib/seccomp-tools/dumper.rb:125: warning: Insecure worl ...

  9. Java中引用类型、对象的创建与销毁

    引用类型 在java中,除了基本数据类型之外的,就是引用数据类型了,引用指的是对象的一个引用,通过引用可以操作对象,控制对象,向对象发送消息. 简单来说,引用可以访问对象的属性,并调用对象的方法 创建 ...

  10. JS 上传图片压缩,原比例压缩

    复制 粘贴 改吧改吧就可用,原生js var fileObj = file.file;//原文件 file是我用vue-vant里的组件,里边有file(原文件)和content(base64) 其它 ...