题目链接

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 == m
  • grid[0].length == n
  • 1 <= m, n <= 40
  • 1 <= k <= m*n
  • grid[i][j] == 0 or 1
  • grid[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动态规划]的更多相关文章

  1. 【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 ...

  2. 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) ...

  3. 【HDU2224】The shortest path(双调欧几里得dp)

    算法导论上一道dp,挺有趣的.于是就研究了一阵. dp(i, j)代表从左边第一个点到第i个点与从从左边最后一个点(即为第一个点)到j点的最优距离和.于是找到了子状态. 决策过程 dp[i][j] = ...

  4. 最短路径遍历所有的节点 Shortest Path Visiting All Nodes

    2018-10-06 22:04:38 问题描述: 问题求解: 本题要求是求遍历所有节点的最短路径,由于本题中是没有要求一个节点只能访问一次的,也就是说可以访问一个节点多次,但是如果表征两次节点状态呢 ...

  5. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  7. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  8. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  9. Shortest Path(思维,dfs)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

随机推荐

  1. sc 使用了配置中心后,如何设置远程和本地配置的优先级

    在 spring 中,如何获取一个 key 的值? applicationContext.getEnvironment().getProperty("swagger.show") ...

  2. SpringBoot整合SpringMVC完成文件上传

    1.编写Controller /** * SPringBoot文件上传 */ //@Controller @RestController //表示该类下的方法的返回值会自动做json格式的转换 pub ...

  3. 时间同步,使用oracle自带的ctss

    crsctl check ctss  --observer mode cluvfy comp clocksync   -检查crss为啥没启用 根据不同版本删除ntp的配置和服务 AIX: stops ...

  4. AWS Cloud Practioner 官方课程笔记 - Part 2

    4. AWS Architecture 设计的5个柱子,也就是5大考量点, Security, Reliability, Performance Efficiency, Cost optimizati ...

  5. Smashing Nodejs 读书笔记(一)

    了不起的Node.js:将JavaScript进行到底 书名:SMASHING Node.js : JavaScript Everywhere 原作者:(美)劳奇 Rauch.G 译者:赵静 出版日期 ...

  6. python 安装成linux中的systemd守护运行

    参考文档1:https://blog.csdn.net/luckytanggu/article/details/53467687 参考文档2:https://www.jianshu.com/p/e14 ...

  7. JAVA总结--jvm

    VM,Virtual Machine 即虚拟机,指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统. JVM,Java Virtual Machine 即Java虚拟机, ...

  8. Visual Studio文件属性

    Visual Studio文件属性主要用到的为:复制到输出目录和生成操作. 复制到输出目录根据选项就可以明白是标示该文件是否输出到生成的目录地址: 生成操作则主要指该文件在执行生成时对文件执行的何种操 ...

  9. HDU 1024 Max Sum Plus Plus (递推)

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. Spring boot集成Swagger,并配置多个扫描路径

    Spring boot集成Swagger,并配置多个扫描路径 1:认识Swagger Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目 ...