forked from maiquynhtruong/algorithms-and-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindows.cpp
80 lines (78 loc) · 2.18 KB
/
windows.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
// Basically the same problem as two rectangles overlapping but now count the number of windows (rectangles) that needs to be
// close in order to close the bottommost window. The close button is at the top right corner
// Input format: First line has N, number of windows. For the next N lines, there are 4 numbers in the ith line. u[i], v[i], x[i], y[i]
// representing upper left coordinates (u[i], v[i]) and lower right coordinatese (x[i], y[i]) of the ith window
// Example input:
// 3
// 1 4 7 1
// 2 5 6 2
// 2 9 5 7
// Example output:
// 1 (Need to close only one window)
// 1 (Which is the first window itself)
// Another example input:
// 9
// 3 6 4 4
// 2 0 5 0
// 1 3 4 1
// 3 9 7 5
// 3 3 7 0
// 0 4 2 3
// 0 4 3 1
// 1 9 2 5
// 0 7 1 0
// Example output:
// 2
// 4 1
// http://ideone.com/qc5ajc
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 100
vector<int> ans;
int cnt = 0;
int x[MAXN], y[MAXN], u[MAXN], v[MAXN];
bool vis[MAXN]; // Not sure about the use of vis because once a window is on top of other windows, it is not likely to be behind
// any of those windows again
int n;
int a[MAXN][MAXN];
bool hidden(int i, int j) { // if window j hides window i
if ((x[i] >= u[j] && x[i] <= x[j]) && (v[i] >= y[j] && v[i] <= v[j]))
return true;
else
return false;
}
void dfs(int i) {
// cout << "Visiting vertex " << i << ", " << u[i] << " " << v[i] << " " << x[i] << " " << y[i] << " " << endl;
cnt++;
vis[i] = true;
ans.push_back(i);
for (int j = i+1; j < n; j++)
// if (vis[j] == false) {
// cout << "vis[" << j << "]=false" << endl;
if (a[i][j] == 1) {
// cout << "a[" << i << "][" << j << "]=true" << endl;
// cout << "Visiting vertext " << j << " next" << endl;
dfs(j);
}
// }
}
int main() {
cin >> n;
for (int i = 0; i < n; i++)
cin >> u[i] >> v[i] >> x[i] >> y[i];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
if (hidden(i, j)) {
a[i][j] = 1;
// cout << j << " hides " << i << endl;
}
vis[i] = false;
}
dfs(0);
sort(ans.begin(), ans.end(), greater<int>());
cout << cnt << endl;
for (vector<int>::iterator it = ans.begin(); it != ans.end(); ++it)
cout << *it+1 << " ";
}