题目链接

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. 20160520—JS打分控件

    效果预览: 可实现功能:鼠标在滑动条内左右滑动,文本框内分数变动:文本框输入数字,滑动条长度自动改变. JavaScript代码: $(function () { scoreFun($("# ...

  2. jmeter之自动重定向和跟随重定向用法

    jmeter工具里面有自动重定向和跟随重定向这2种选择,那么他们到底有啥区别呢? 目录 1.自动重定向和跟随重定向 2.举个例子 1.自动重定向和跟随重定向 01.3XX的请求一般要使用跟随重定向,2 ...

  3. 视频格式转换.ZC资料

    1.20191013: ZC:这些都是 2015年做的尝试,之前貌似没有记录下来,现在 再次用到,把用到的记录下来: ZC: (1) 使用的视频格式转换工具是 "??/XiGua Yings ...

  4. Leveldb源码分析--1

    coming from http://blog.csdn.net/sparkliang/article/details/8567602 [前言:看了一点oceanbase,没有意志力继续坚持下去了,暂 ...

  5. [Python3 填坑] 008 索引君的朋友 in

    目录 1. print( 坑的信息 ) 2. 开始填坑 (1) 前情提要 (2) 索引君的朋友 in 上线 (3) 既然说了 in,不妨再说一说 not in (4) 一些补充 1. print( 坑 ...

  6. 十二、支持向量机(Support Vector Machines)

    12.1 优化目标 参考视频: 12 - 1 - Optimization Objective (15 min).mkv 到目前为止,你已经见过一系列不同的学习算法.在监督学习中,许多学习算法的性能都 ...

  7. controller函数中参数列表使用多个@RequestBody

    首先出现这种情况是因为有下面这种需求 $.ajax({ type: "POST", url: "${pageContext.request.contextPath}/co ...

  8. Elasticsearch7.X 入门学习第九课笔记-----聚合分析Aggregation

    原文:Elasticsearch7.X 入门学习第九课笔记-----聚合分析Aggregation 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. ...

  9. asp.net 关于SessionId

    原文:https://www.cnblogs.com/zhang1999/p/7278020.html 登陆页面使用Session存储验证码,导致会话产生SessionId,从而导致会话固定,登陆后用 ...

  10. netcore项目使用swagger开发

    首先我创建一个netcore项目,我使用的工具是vs2019 这里需要注意的是,看情况选择是否开启身份验证,一般是没有需求的,这里因为我是测试使用所以需要取消勾兑为https配置,并且我没有启用doc ...