-
Notifications
You must be signed in to change notification settings - Fork 0
/
0847.cpp
34 lines (32 loc) · 1.12 KB
/
0847.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
class Solution {
public:
int shortestPathLength(vector<vector<int>>& graph) {
if (graph.empty()) return 0;
queue<pair<int, int>> q;
// unordered_set<string> visited;
int size = graph.size();
vector<vector<int>> visited(size, vector<int>(1 << size));
const int kAns = (1 << size) - 1;
for (int i=0; i<size; i++) q.push({i, 1<<i});
int step = 0;
while (!q.empty()) {
int q_size = q.size();
while(q_size--) {
auto cur_pair = q.front(); q.pop();
int cur = cur_pair.first;
int state = cur_pair.second;
if (state == kAns) return step;
// string key = to_string(cur)+to_string(state);
// if (visited.count(key)) continue;
// visited.insert(key);
if (visited[cur][state]) continue;
visited[cur][state] = 1;
for (int nei: graph[cur]) {
q.push({nei, (1 << nei)|state});
}
}
step += 1;
}
return -1;
}
};