-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightson.cpp
73 lines (53 loc) · 1.33 KB
/
lightson.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
#include <bits/stdc++.h>
using namespace std;
using pi = pair<int, int>;
const int maxn = 100 + 8;
const int maxm = 2e4 + 8;
int n, m;
set<pi> lights[maxn][maxn];
bool handled[maxn][maxn];
bool on[maxn][maxn];
bool visited[maxn][maxn];
int lights_on = 1;
bool all_handled = false;
void f_fill(int x, int y) {
if (x < 0 || x >= n || y < 0 || y >= n) return;
if (visited[x][y]) return;
if (!on[x][y]) return;
visited[x][y] = true;
if (!handled[x][y]) {
all_handled = false;
handled[x][y] = true;
for (pi l : lights[x][y]) {
if (!on[l.first][l.second]) {
on[l.first][l.second] = true;
lights_on++;
}
}
}
f_fill(x + 1, y);
f_fill(x - 1, y);
f_fill(x, y + 1);
f_fill(x, y - 1);
}
int main() {
ifstream fin("lightson.in");
ofstream fout("lightson.out");
fin >> n >> m;
memset(handled, false, sizeof(handled));
memset(on, false, sizeof(on));
for (int i = 0; i < m; i++) {
int x, y, a, b;
fin >> x >> y >> a >> b;
x--, y--, a--, b--;
lights[x][y].insert({a, b});
}
on[0][0] = true;
while (!all_handled) {
all_handled = true;
memset(visited, false, sizeof(visited));
f_fill(0, 0);
}
fout << lights_on << endl;
return 0;
}