forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
187 lines (155 loc) · 5.77 KB
/
main.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/// Source : https://leetcode.com/problems/shortest-path-to-get-all-keys/description/
/// Author : liuyubobobo
/// Time : 2018-07-09
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <queue>
using namespace std;
/// Brute Force all Keys Permutation
/// Time Complexity: O(keys!*keys*n^2)
/// Space Complexity:O(keys + n^2)
class Solution {
private:
int n, m;
const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public:
int shortestPathAllKeys(vector<string>& grid) {
n = grid.size();
m = grid[0].size();
unordered_map<char, pair<int, int>> poi = getPOI(grid);
vector<char> keys = getAllKeys(grid);
sort(keys.begin(), keys.end());
int res = INT_MAX;
do{
res = min(res, go(grid, keys, poi, res));
}while(next_permutation(keys.begin(), keys.end()));
if(res == INT_MAX)
return -1;
return res;
}
private:
int go(const vector<string>& grid, const vector<char>& keys,
unordered_map<char, pair<int, int>>& poi, int curMinRes){
vector<pair<int, int>> pos = {poi['@']};
for(char key: keys)
pos.push_back(poi[key]);
int res = 0;
unordered_set<char> ownKeys;
for(int i = 1; i < pos.size() ; i ++){
int need = go(grid, pos[i-1], pos[i], ownKeys);
if(need == -1)
return INT_MAX;
res += need;
if(res >= curMinRes)
return INT_MAX;
ownKeys.insert(keys[i-1]);
}
return res;
}
int go(const vector<string>& grid,
const pair<int, int>& start, const pair<int, int>& end,
const unordered_set<char>& ownKeys){
queue<pair<pair<int, int>, int>> q;
q.push(make_pair(start, 0));
unordered_set<int> visited;
visited.insert(start.first * m + start.second);
while(!q.empty()){
pair<int, int> cur = q.front().first;
int step = q.front().second;
q.pop();
for(int i = 0 ; i < 4 ; i ++){
int nextX = cur.first + d[i][0];
int nextY = cur.second + d[i][1];
if(nextX == end.first && nextY == end.second)
return step + 1;
if(inArea(nextX, nextY) && grid[nextX][nextY] != '#' &&
visited.find(nextX * m + nextY) == visited.end()){
if(!isLock(grid[nextX][nextY]) ||
(isLock(grid[nextX][nextY]) && ownKeys.find(grid[nextX][nextY] - 'A' + 'a') != ownKeys.end())){
q.push(make_pair(make_pair(nextX, nextY), step + 1));
visited.insert(nextX * m + nextY);
}
}
}
}
return -1;
}
vector<char> getAllKeys(const vector<string>& grid){
vector<char> res;
for(int i = 0 ; i < n ; i ++)
for(int j = 0 ; j < m ; j ++)
if(isKey(grid[i][j]))
res.push_back(grid[i][j]);
return res;
}
unordered_map<char, pair<int, int>> getPOI(const vector<string>& grid){
unordered_map<char, pair<int, int>> res;
for(int i = 0 ; i < n ; i ++)
for(int j = 0 ; j < m ; j ++)
if(grid[i][j] != '.' && grid[i][j] != '#'){
assert(grid[i][j] == '@' || isKey(grid[i][j]) || isLock(grid[i][j]));
assert(res.find(grid[i][j]) == res.end());
res[grid[i][j]] = make_pair(i, j);
}
return res;
};
bool isKey(char c){
return c >= 'a' && c <= 'z';
}
bool isLock(char c){
return c >= 'A' && c <= 'Z';
}
bool inArea(int x, int y){
return x >= 0 && x < n && y >= 0 && y < m;
}
};
int main() {
vector<string> grid1 = {"@.a.#",
"###.#",
"b.A.B"};
cout << Solution().shortestPathAllKeys(grid1) << endl;
vector<string> grid2 = {"@..aA",
"..B#.",
"....b"};
cout << Solution().shortestPathAllKeys(grid2) << endl;
vector<string> grid3 = {"@abcdeABCDEFf"};
cout << Solution().shortestPathAllKeys(grid3) << endl;
vector<string> grid4 = {
"#..#.#.#..#.#.#.....#......#..",
".#.......#....#A.....#.#......",
"#....#.....#.........#........",
"...#.#.........#..@....#....#.",
".#.#.##...#.........##....#..#",
"..........#..#..###....##..#.#",
".......#......#...#...#.....c#",
".#...#.##......#...#.###...#..",
"..........##...#.......#......",
"#...#.........a#....#.#.##....",
"..#..#...#...#..#....#.....##.",
"..........#...#.##............",
"...#....#..#.........#..D.....",
"....#E.#....##................",
"...........##.#.......#.#....#",
"...#..#...#.#............#e...",
"..#####....#.#...........##..#",
"##......##......#.#...#..#.#..",
".#F.......#..##.......#....#..",
"............#....#..#..#...#..",
".............#...#f...#..##...",
"....#..#...##.........#..#..#.",
".....#.....##.###..##.#......#",
".#..#.#...#.....#........###..",
".....#.#...#...#.....#.....#..",
"##.....#....B.....#..#b.......",
".####....##..#.##..d.#......#.",
"..#.....#....##........##...##",
"...#...#...C..#..#....#.......",
"#.....##.....#.#......#......."
};
cout << Solution().shortestPathAllKeys(grid4) << endl;
return 0;
}