-
Notifications
You must be signed in to change notification settings - Fork 3
/
10309-Turnthelightsoff.cpp
executable file
·101 lines (91 loc) · 1.59 KB
/
10309-Turnthelightsoff.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// 10309 - Turn the lights off.cpp
// Uva
//
// Created by Alexander Faxå on 2012-05-19.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <vector>
bool grid[10][10];
bool secondgrid[10][10];
using namespace std;
void flip(int x, int y)
{
grid[x][y] = !grid[x][y];
if(x)
grid[x-1][y] = !grid[x-1][y];
if(x != 9)
grid[x+1][y] = !grid[x+1][y];
if(y)
grid[x][y-1] = !grid[x][y-1];
if(y != 9)
grid[x][y+1] = !grid[x][y+1];
}
int main()
{
string s;
while(true)
{
cin >> s;
if(s=="end")
break;
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++)
{
char c;
cin >> c;
if(c=='#')
grid[i][j] = false;
else
grid[i][j] = true;
secondgrid[i][j] = grid[i][j];
}
int mi = 0x7FFFFFFF;
vector<bool> v(10, false);
for(int i = 0; i < 1024; ++i)
{
for(int i = 0; i < 10; i++)
for(int j = 0; j < 10; j++)
grid[i][j] = secondgrid[i][j];
for(vector<bool>::iterator it = v.begin(); it != v.end(); ++it)
{
if(*it)
*it = false;
else{
*it = true;
break;
}
}
int current = 0;
for(int j = 0; j < 10; j++)
if(v[j])
{
flip(0,j);
current++;
}
for(int j = 1; j < 10; j++)
for(int k = 0; k < 10; k++)
{
if(grid[j-1][k])
{
flip(j,k);
current++;
}
}
bool ok = true;
for(int j = 0; j < 10; j++)
if(grid[9][j])
ok = false;
if(ok)
mi = min(mi, current);
}
cout << s << " ";
if(mi <= 100)
cout << mi << endl;
else
cout << "-1" << endl;
}
return 0;
}