[LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径

We are given a 2-dimensional?grid.?"."?is an empty cell,?"#"?is?a wall,?"@"?is the starting point, ("a",?"b", ...) are keys, and ("A",?"B", ...) are locks.

We start at the starting point, and one move consists of walking one space in one of the 4 cardinal directions.? We cannot walk outside the grid, or walk into a wall.? If we walk over a key, we pick it up.? We can‘t walk over a lock unless we have the corresponding key.

For some?1 <= K <= 6, there is exactly one lowercase and one uppercase letter of the first?Kletters of the English alphabet in the grid.? This means that there is exactly one key for each lock, and one lock for each key; and also that the letters used to represent the keys and locks were?chosen in the same order as the English alphabet.

Return the lowest number of moves to acquire all keys.? If?it‘s impossible, return?-1.

Example 1:

Input: ["@.a.#","###.#","b.A.B"]
Output: 8

Example 2:

Input: ["@..aA","..B#.","....b"]
Output: 6

Note:

  1. 1 <= grid.length?<= 30
  2. 1 <= grid[0].length?<= 30
  3. grid[i][j]?contains only?‘.‘,?‘#‘,?‘@‘,?‘a‘-``‘f``‘?and?‘A‘-‘F‘
  4. The number of keys is in?[1, 6].? Each key has a different letter and opens exactly one lock.

Github 同步地址:

https://github.com/grandyang/leetcode/issues/864

参考资料:

https://leetcode.com/problems/shortest-path-to-get-all-keys/

LeetCode All in One 题目讲解汇总(持续更新中...)

原文地址:https://www.cnblogs.com/grandyang/p/11219780.html

时间: 2024-10-15 05:01:26

[LeetCode] 864. Shortest Path to Get All Keys 获得所有钥匙的最短路径的相关文章

[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

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

BFS 基础写法 —— 以 LeetCode #1091 Shortest Path in Binary Matrix 为例

Question In an N by N square grid, each cell is either empty (0) or blocked (1). A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that: Adjacent cells C_i and C_{i+1} are connected

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

leetcode 934. Shortest Bridge

The algorithm for this problem is not so hard to figure out. This is a problem of finding the length of the shortest path in graph. As I mentioned in my previous article, BFS is a good way to slove this kind of problem. This problem is a little diffe

LeetCode --- 64. Minimum Path Sum

题目链接:Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 这道题的要求是在m*n

【Leetcode】Minimum Path Sum

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

【LeetCode】Simplify Path

Simplify Path Given an absolute path for a file (Unix-style), simplify it. Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

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