-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path10309 - Turn the Lights Off
52 lines (48 loc) · 1023 Bytes
/
10309 - Turn the Lights Off
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
// UVa 10309 - Turn the Lights Off
// by Rico Tiongson
// C++ 0.000s 06/10/16
#include <bits/stdc++.h>
#define forn(i, a, n) for (int i = a; i < n; ++i)
using namespace std;
char nm[1000];
char g[10][11], c[10][11];
int dx[] = {0, -1, 0, 1, 0};
int dy[] = {0, 0, -1, 0, 1};
void toggle(int i, int j) {
forn (k, 0, 5) {
int x = i + dx[k];
int y = j + dy[k];
if (x >= 0 && x < 10 && y >= 0 && y < 10)
c[x][y] = (c[x][y] == '#' ? 'O' : '#');
}
}
int main() {
while (scanf("%s", nm) == 1 && strcmp(nm, "end")) {
forn (i, 0, 10) scanf("%s", g[i]);
int mn = 101;
forn (mask, 0, 1 << 10) {
int toggles = 0;
memcpy(c, g, sizeof g);
forn (j, 0, 10) {
if ((1 << j) & mask) {
toggle(0, j);
toggles++;
}
}
forn (i, 1, 10) {
forn (j, 0, 10) {
if (c[i-1][j] == 'O') {
toggle(i, j);
toggles++;
}
}
}
forn (j, 0, 10)
if (c[9][j] == 'O')
goto loop;
mn = min(mn, toggles);
loop:;
}
printf("%s %d\n", nm, mn == 101 ? -1 : mn);
}
}