Problem
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Ā K
Ā letters 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 <= grid.lengthĀ <= 30
1 <= grid[0].lengthĀ <= 30
grid[i][j]
Ā contains onlyĀ '.'
,Ā'#'
,Ā'@'
,Ā'a'-
'f
'
Ā andĀ'A'-'F'
- The number of keys is inĀ
[1, 6]
.Ā Each key has a different letter and opens exactly one lock.
Solution: BFS
Time complexity: O(m*n*64)
Space complexity: O(m*n*64)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
// Author: Huahua // Running time: 8 ms class Solution { public: int shortestPathAllKeys(vector<string>& grid) { int m = grid.size(); int n = grid[0].size(); int all_keys = 0; queue<int> q; vector<vector<vector<int>>> seen(m, vector<vector<int>>(n, vector<int>(64, 0))); // Init for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) { const char c = grid[i][j]; if (c == '@') { q.push((j << 16) | (i << 8)); seen[i][j][0] = 1; } else if (c >= 'a' && c <= 'f') { all_keys |= (1 << (c - 'a')); } } const vector<int> dirs{-1, 0, 1, 0, -1}; int steps = 0; while (!q.empty()) { int size = q.size(); while (size--) { int s = q.front(); q.pop(); int x = s >> 16; int y = (s >> 8) & 0xFF; int keys = s & 0xFF; if (keys == all_keys) return steps; for (int i = 0; i < 4; ++i) { int nkeys = keys; int nx = x + dirs[i]; int ny = y + dirs[i + 1]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue; const char c = grid[ny][nx]; if (c == '#') continue; // Wall // Do not have the key. if (c >= 'A' && c <= 'F' && !(keys & (1 << (c - 'A')))) continue; // Update the keys we have. if (c >= 'a' && c <= 'f') nkeys |= (1 << (c - 'a')); if (seen[ny][nx][nkeys]) continue; q.push((nx << 16) | (ny << 8) | nkeys); seen[ny][nx][nkeys] = 1; } } ++steps; } return -1; } }; |
Python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# Author: Huahua, 390 ms class Solution: def shortestPathAllKeys(self, grid): m, n = len(grid), len(grid[0]) all_keys = 0 seen = [[[None]* 64 for _ in range(n)] for _ in range(m)] q = collections.deque() for i in range(m): for j in range(n): c = grid[i][j] if c == '@': q.append((j << 16) | (i << 8)) seen[i][j][0] = 1 elif c >= 'a' and c <= 'f': all_keys |= (1 << (ord(c) - ord('a'))) dirs = [-1, 0, 1, 0, -1] steps = 0 while q: size = len(q) while size > 0: size -= 1 s = q.popleft() x = s >> 16 y = (s >> 8) & 0xff keys = s & 0xff if keys == all_keys: return steps for i in range(4): nx, ny, nkeys = x + dirs[i], y + dirs[i + 1], keys if nx < 0 or nx >= n or ny < 0 or ny >= m: continue c = grid[ny][nx] if c == '#': continue if c in string.ascii_uppercase and keys & (1 << (ord(c) - ord('A'))) == 0: continue if c in string.ascii_lowercase: nkeys |= (1 << (ord(c) - ord('a'))) if seen[ny][nx][nkeys]: continue q.append((nx << 16) | (ny << 8) | nkeys) seen[ny][nx][nkeys] = 1 steps += 1 return -1 |
Related Problems