-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path1691.cc
74 lines (64 loc) · 2.12 KB
/
1691.cc
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
#include <iostream>
#include <vector>
#include <queue>
#include <complex>
#include <climits>
#include <cstdio>
using namespace std;
struct Rectangle {
int top, left, bottom, right, color;
Rectangle() {}
Rectangle(int t, int l, int b, int r, int c) : top(t), left(l), bottom(b), right(r), color(c){}
};
int main() {
int cases;
cin >> cases;
while(cases--) {
int N;
cin >> N;
vector<Rectangle> rectangles;
for(int i = 0; i < N; ++i) {
int ly, lx, ry, rx, color;
cin >> ly >> lx >> ry >> rx >> color;
rectangles.push_back(Rectangle(ly, lx, ry, rx, color));
}
vector<unsigned> upper(N, 0);
for(int i = 0; i < N; ++i) {
unsigned upper_flg = 0;
for(int j = 0; j < N; ++j) {
if(rectangles[j].bottom == rectangles[i].top &&
rectangles[j].left <= rectangles[i].right && rectangles[j].right >= rectangles[i].left)
{
upper_flg |= (1<<j);
}
}
upper[i] = upper_flg;
}
vector<vector<bool> > visited(1<<N, vector<bool>(21, false));
priority_queue<pair<int, pair<unsigned, int> > > q;
q.push(make_pair(0, make_pair(0, 0)));
while(!q.empty()) {
int cost = q.top().first;
unsigned state = q.top().second.first;
int color = q.top().second.second;
q.pop();
if(visited[state][color]) continue;
visited[state][color] = true;
if(state == (1<<N)-1) {
cout << -cost << endl;
break;
}
for(int i = 0; i < N; ++i) {
if(state & (1<<i)) continue;
if((state & upper[i]) != upper[i]) continue;
if(rectangles[i].color == color) {
q.push(make_pair(cost, make_pair(state | (1<<i), color)));
}
else {
q.push(make_pair(cost-1, make_pair(state | (1<<i), rectangles[i].color)));
}
}
}
}
return 0;
}