|
| 1 | +package problem0864 |
| 2 | + |
| 3 | +/* |
| 4 | +You are given an m x n grid grid where: |
| 5 | + '.' is an empty cell. |
| 6 | + '#' is a wall. |
| 7 | + '@' is the starting point. |
| 8 | + Lowercase letters represent keys. |
| 9 | + Uppercase letters represent locks. |
| 10 | +You start at the starting point and one move consists of walking one space in one of the four cardinal directions. |
| 11 | +You cannot walk outside the grid, or walk into a wall. |
| 12 | +If you walk over a key, you can pick it up and you cannot walk over a lock unless you have its corresponding key. |
| 13 | +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. |
| 14 | +This means that there is exactly one key for each lock, and one lock for each key; |
| 15 | +and also that the letters used to represent the keys and locks were chosen in the same order as the English alphabet. |
| 16 | +Return the lowest number of moves to acquire all keys. If it is impossible, return -1. |
| 17 | +*/ |
| 18 | + |
| 19 | +var moves = [4][2]int{ |
| 20 | + {1, 0}, {0, 1}, {-1, 0}, {0, -1}, |
| 21 | +} |
| 22 | + |
| 23 | +type Path struct { |
| 24 | + X, Y int |
| 25 | + Keys uint32 |
| 26 | +} |
| 27 | + |
| 28 | +func shortestPathAllKeys(grid []string) int { |
| 29 | + var res int |
| 30 | + sx, sy, dst := FindStartAndKeys(grid) |
| 31 | + var cur, nxt []Path |
| 32 | + cur = []Path{{sx, sy, 0}} |
| 33 | + var cache = makeCache(grid) |
| 34 | + for len(cur) > 0 { |
| 35 | + nxt = nxt[:0] |
| 36 | + res++ |
| 37 | + for _, c := range cur { |
| 38 | + if c.Keys == dst { |
| 39 | + return res - 2 |
| 40 | + } |
| 41 | + nxt = append(nxt, NextCells(grid, cache, c)...) |
| 42 | + } |
| 43 | + cur, nxt = nxt, cur |
| 44 | + } |
| 45 | + return -1 |
| 46 | +} |
| 47 | + |
| 48 | +func makeCache(grid []string) [][]map[uint32]bool { |
| 49 | + var res = make([][]map[uint32]bool, len(grid)) |
| 50 | + for i := range grid { |
| 51 | + res[i] = make([]map[uint32]bool, len(grid[0])) |
| 52 | + for j := range grid[i] { |
| 53 | + res[i][j] = map[uint32]bool{} |
| 54 | + } |
| 55 | + } |
| 56 | + return res |
| 57 | +} |
| 58 | + |
| 59 | +func NextCells(grid []string, cache [][]map[uint32]bool, cur Path) []Path { |
| 60 | + var res = make([]Path, 0, 4) |
| 61 | + if isOutside(grid, cur.X, cur.Y) { |
| 62 | + return nil |
| 63 | + } |
| 64 | + if cache[cur.X][cur.Y][cur.Keys] { |
| 65 | + return nil |
| 66 | + } |
| 67 | + if grid[cur.X][cur.Y] == '#' { |
| 68 | + return nil |
| 69 | + } |
| 70 | + if isUpper(grid[cur.X][cur.Y]) && !cur.HasKey(grid[cur.X][cur.Y]) { |
| 71 | + return nil |
| 72 | + } |
| 73 | + if isLower(grid[cur.X][cur.Y]) { |
| 74 | + cur.AddKey(grid[cur.X][cur.Y]) |
| 75 | + } |
| 76 | + cache[cur.X][cur.Y][cur.Keys] = true |
| 77 | + for _, move := range moves { |
| 78 | + if !isOutside(grid, cur.X, cur.Y) { |
| 79 | + res = append(res, Path{cur.X + move[0], cur.Y + move[1], cur.Keys}) |
| 80 | + } |
| 81 | + } |
| 82 | + return res |
| 83 | +} |
| 84 | + |
| 85 | +func isOutside(grid []string, x, y int) bool { |
| 86 | + return x < 0 || x >= len(grid) || y < 0 || y >= len(grid[0]) |
| 87 | +} |
| 88 | + |
| 89 | +// FindStartAndKeys finds the starting position and the desired keys |
| 90 | +func FindStartAndKeys(grid []string) (int, int, uint32) { |
| 91 | + var sx, sy int |
| 92 | + var key Path |
| 93 | + for i := range grid { |
| 94 | + for j := range grid[i] { |
| 95 | + switch grid[i][j] { |
| 96 | + case '@': |
| 97 | + sx, sy = i, j |
| 98 | + case '.', '#': |
| 99 | + default: |
| 100 | + if isLower(grid[i][j]) { |
| 101 | + key.AddKey(grid[i][j]) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return sx, sy, key.Keys |
| 107 | +} |
| 108 | + |
| 109 | +func isUpper(letter byte) bool { |
| 110 | + return letter >= 'A' && letter <= 'Z' |
| 111 | +} |
| 112 | + |
| 113 | +func isLower(letter byte) bool { |
| 114 | + return letter >= 'a' && letter <= 'z' |
| 115 | +} |
| 116 | + |
| 117 | +func (p *Path) AddKey(key byte) { |
| 118 | + p.Keys |= 1 << (key - 'a') |
| 119 | +} |
| 120 | + |
| 121 | +func (p *Path) HasKey(lock byte) bool { |
| 122 | + var bit uint32 = 1 << (lock - 'A') |
| 123 | + return (p.Keys & bit) == bit |
| 124 | +} |
0 commit comments