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 == 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[4][2] = {-1,0, 0,1,1,0,0,-1};
int dp[41][41][1700];
bool flag[41][41];
class Solution {
public:
    vector<vector<int>> _grid;

    bool inside(int x, int y){
        if(x>=0 && x<_grid.size() && y>=0 &&y<_grid[0].size())
            return true;
        return false;
    }

    int shortestPath(vector<vector<int>>& grid, int k) {
        _grid = grid;
        memset(dp, -1, sizeof(dp));
        memset(flag, 0 ,sizeof(flag));
        int ret = dfs(0, 0, k);
        return ret>=INT_MAX/2 ? -1 : ret;
    }

    int dfs(int x, int y, int k){
        if(k<0)
            return INT_MAX/2;
        if(dp[x][y][k] != -1)
            return dp[x][y][k];
        if(x==_grid.size()-1 && y==_grid[0].size()-1)
            return dp[x][y][k] = 0;

        int ret = INT_MAX/2;
        for(int i=0; i<4; i++){
            int xx = x+dirs[i][0];
            int yy = y+dirs[i][1];
            if(inside(xx, yy) && !flag[xx][yy]){
                flag[xx][yy] = true;
                int temp = INT_MAX/2;
                if(_grid[xx][yy])
                    temp = dfs(xx, yy, k-1);
                else
                    temp = dfs(xx, yy, k);
                ret = min(ret, 1+temp);
                flag[xx][yy] = false;
            }
        }
        return dp[x][y][k] = ret;
    }
};

原文地址:https://www.cnblogs.com/jasonlixuetao/p/12046501.html

时间: 2024-11-08 21:41:52

leetcode_1293. Shortest Path in a Grid with Obstacles Elimination_[dp动态规划]的相关文章

LeetCode 1293. Shortest Path in a Grid with Obstacles Elimination

原题链接在这里:https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/ 题目: 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

[LeetCode 1368] Minimum Cost to Make at Least One Valid Path in a Grid

Given a m x n grid. Each cell of the grid has a sign pointing to the next cell you should visit if you are currently in this cell. The sign of grid[i][j] can be: 1 which means go to the cell to the right. (i.e go from grid[i][j] to grid[i][j + 1]) 2 

Method for finding shortest path to destination in traffic network using Dijkstra algorithm or Floyd-warshall algorithm

A method is presented for finding a shortest path from a starting place to a destination place in a traffic network including one or more turn restrictions, one or more U-turns and one or more P-turns using a Dijkstra algorithm. The method as sets a

The Shortest Path in Nya Graph HDU - 4725

Problem Description This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.The Nya graph is an un

OSPF(Open Shortest Path First)

---恢复内容开始--- 1.概述 路由协议OSPF全称为Open Shortest Path First,也就开放的最短路径优先协议,因为OSPF是由IETF开发的. OSPF的流量使用IP协议号89. OSPF对网络没有跳数限制,支持 Classless Interdomain Routing (CIDR)和Variable-Length Subnet Masks (VLSMs),没有自动汇总功能. OSPF并不会周期性更新路由表,而采用增量更新,即只在路由有变化时,才会发送更新,并且只发送

HDU 4725 The Shortest Path in Nya Graph(构图)

The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13445    Accepted Submission(s): 2856 Problem Description This is a very easy problem, your task is just calculate

干货 | 列生成VRPTW子问题ESPPRC( Elementary shortest path problem with resource constraints)介绍附C++代码

00 前言 各位小伙伴大家好,相信大家已经看过前面column generation求解vehicle routing problems的过程详解.该问题中,子问题主要是找到一条reduced cost最小的合法路径,然后加入到Master Problem中.其实,子问题也是一个著名的NP-Hard问题,今天我们就来介绍一下. 01 ESPPRC 考虑图1.1中描述的网络. 除了每条边的成本c_ij之外,还存在经过边(i,j)的所消耗的资源t_ij,比如时间. 我们的目标是找到从开始节点到结束节

[LeetCode] 847. Shortest Path Visiting All Nodes 访问所有结点的最短路径

An undirected, connected graph of N nodes (labeled?0, 1, 2, ..., N-1) is given as?graph. graph.length = N, and?j != i?is in the list?graph[i]?exactly once, if and only if nodes?i?and?j?are connected. Return the length of the shortest path that visits

Dijkstra’s Shortest Path Algorithm / LeetCode 787. Cheapest Flights Within K Stops

Dijkstra’s Shortest Path Algorithm 实现详见:https://www.geeksforgeeks.org/dijkstras-shortest-path-algorithm-using-priority_queue-stl/ 需要注意的是,priority_queue并无法更新内部的元素,因此我们更新dist的同时,直接把新的距离加入pq即可.pq里虽然有outdated的dist,但是由于距离过长,他们并不会更新dist. // If there is sho