forked from t3nsor/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cerc07b.cpp
70 lines (70 loc) · 1.07 KB
/
cerc07b.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
// 2009-09-26
#include <cstdio>
#include <set>
#include <cstring>
using namespace std;
bool a[20][20];
bool b[20][20];
void flip(int x,int y)
{
b[x][y]=!b[x][y];
b[x+1][y]=!b[x+1][y];
b[x][y+1]=!b[x][y+1];
if (x>0)
b[x-1][y]=!b[x-1][y];
if (y>0)
b[x][y-1]=!b[x][y-1];
}
int main()
{
int X,Y,i,j,k;
for (;;)
{
scanf("%d %d",&Y,&X);
if (X==0&&Y==0) break;
char s[20];
for (i=0; i<Y; i++)
{
scanf("%s",&s);
for (j=0; j<X; j++)
if (s[j]=='X')
a[j][i]=true;
else
a[j][i]=false;
}
int minflips=1000;
for (i=0; i<(1<<X); i++)
{
memcpy(b,a,sizeof(b));
int flips=0;
for (j=0; j<X; j++)
if ((1<<j)&i)
{
flips++;
flip(j,0);
}
for (j=1; j<Y; j++)
for (k=0; k<X; k++)
if (b[k][j-1])
{
flip(k,j);
flips++;
}
bool B=true;
for (j=0; j<X; j++)
if (b[j][Y-1])
{
B=false;
break;
}
if (B)
if (flips<minflips)
minflips=flips;
}
if (minflips<1000)
printf("You have to tap %d tiles.\n",minflips);
else
printf("Damaged billboard.\n");
}
return 0;
}