leetcode_1293. Shortest Path in a Grid with Obstacles Elimination_[dp动态规划]
Given a m * n grid, where each cell is either 0 (empty) or 1 (obstacle). In one step, you can move up, down, left or right from and to an empty cell.
Return the minimum number of steps to walk from the upper left corner (0, 0) to the lower right corner (m-1, n-1) given that you can eliminate at most k obstacles. If it is not possible to find such walk return -1.
Example 1:
Input:
grid =
[[0,0,0],
[1,1,0],
[0,0,0],
[0,1,1],
[0,0,0]],
k = 1
Output: 6
Explanation:
The shortest path without eliminating any obstacle is 10.
The shortest path with one obstacle elimination at position (3,2) is 6. Such path is(0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (3,2) -> (4,2).
Example 2:
Input:
grid =
[[0,1,1],
[1,1,1],
[1,0,0]],
k = 1
Output: -1
Explanation:
We need to eliminate at least two obstacles to find such a walk.
Constraints:
grid.length == mgrid[0].length == n1 <= m, n <= 401 <= k <= m*ngrid[i][j] == 0 or 1grid[0][0] == grid[m-1][n-1] == 0
题意:给定一个矩阵,能向上下左右移动,问从[0, 0]走到[n-1, m-1]且在途中最多消除k个障碍的最少步数。
解法:很明显的动态规划题。朴素dfs会有大量重复的计算,使用动态规划存贮已经计算过的结果,避免重复计算。
int dirs[][] = {-,, ,,,,,-};
int dp[][][];
bool flag[][];
class Solution {
public:
vector<vector<int>> _grid;
bool inside(int x, int y){
if(x>= && x<_grid.size() && y>= &&y<_grid[].size())
return true;
return false;
}
int shortestPath(vector<vector<int>>& grid, int k) {
_grid = grid;
memset(dp, -, sizeof(dp));
memset(flag, ,sizeof(flag));
int ret = dfs(, , k);
return ret>=INT_MAX/ ? - : ret;
}
int dfs(int x, int y, int k){
if(k<)
return INT_MAX/;
if(dp[x][y][k] != -)
return dp[x][y][k];
if(x==_grid.size()- && y==_grid[].size()-)
return dp[x][y][k] = ;
int ret = INT_MAX/;
for(int i=; i<; i++){
int xx = x+dirs[i][];
int yy = y+dirs[i][];
if(inside(xx, yy) && !flag[xx][yy]){
flag[xx][yy] = true;
int temp = INT_MAX/;
if(_grid[xx][yy])
temp = dfs(xx, yy, k-);
else
temp = dfs(xx, yy, k);
ret = min(ret, +temp);
flag[xx][yy] = false;
}
}
return dp[x][y][k] = ret;
}
};
leetcode_1293. Shortest Path in a Grid with Obstacles Elimination_[dp动态规划]的更多相关文章
- 【leetcode】1293 .Shortest Path in a Grid with Obstacles
You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You ...
- LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination
题目 非常简单的BFS 暴搜 struct Node { int x; int y; int k; int ans; Node(){} Node(int x,int y,int k,int ans) ...
- 【HDU2224】The shortest path(双调欧几里得dp)
算法导论上一道dp,挺有趣的.于是就研究了一阵. dp(i, j)代表从左边第一个点到第i个点与从从左边最后一个点(即为第一个点)到j点的最优距离和.于是找到了子状态. 决策过程 dp[i][j] = ...
- 最短路径遍历所有的节点 Shortest Path Visiting All Nodes
2018-10-06 22:04:38 问题描述: 问题求解: 本题要求是求遍历所有节点的最短路径,由于本题中是没有要求一个节点只能访问一次的,也就是说可以访问一个节点多次,但是如果表征两次节点状态呢 ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
随机推荐
- Delphi XE2 之 FireMonkey 入门(22) - 数据绑定: BindingSource、BindingName、FindBinding()、Binding[]
在窗体上添加 TrackBar1.Edit1.Label1, 然后设置属性(可在设计时): procedure TForm1.FormCreate(Sender: TObject); begin ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_02 递归_1_递归概念&分类&注意事项
a方法里面调用自己,但是没有停止的条件 方法没有停止的条件. 栈内存溢出的异常. 只有栈,没有堆内存 先执行main方法压栈执行 main方法里面调用a方法.a方法就会压栈 改成20000
- jmeter接口测试初体验
今天初体验了一把jmeter,把操作的一些经历贴出来,督促自己进步.等逐步掌握后再次回首时,希望是有所思的,欣慰的! jmeter: Apache JMeter是Apache组织开发的基于Java的压 ...
- dict用法
1 dict.items() https://www.runoob.com/python3/python3-att-dictionary-items.html 2 setdefault的用法 注意se ...
- java二周的学习总结
一转眼二周就过去了,个人觉得虽然java和C语言有差异,但差别并不大,因为语法语句方面都是差不多的,因为我上个学期并没有很认真的学好C语言,所以我这个学期更希望学好java,java方面还是挺有趣的, ...
- Redis进阶:Redis的持久化机制
Redis进阶:Redis的持久化机制 Redis的持久化机制目前包括RBD和AOF两种方式. RDB持久化 RDB持久化方式是在指定的时间间隔对数据进行快照存储.过期的键值不会被存储到快照中.如果恢 ...
- 如何创建Windows虚拟机
Windows虚拟机搭建 第1步:运行"Vmware WorkStation",看到主页面,创建新的虚拟机 第2步:新建虚拟机向导——典型(推荐) 第3步:选择光盘映像文件 第4步 ...
- Maven 标准项目结构
项目结构 src main java 源文件 resources 资源文件 filters 资源过滤文件 config 配置文件 scripts 脚本文件 webap ...
- Maven父项目 以SpringBoot项目为例
父项目pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...
- STM32 debug setting 闪退
闪退user文件夹里旧版本文件 有个同名的 uvprojt类型的文件删了即可