-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCERC07B.cpp
More file actions
104 lines (97 loc) · 1.6 KB
/
CERC07B.cpp
File metadata and controls
104 lines (97 loc) · 1.6 KB
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
102
103
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <math.h>
#include <map>
using namespace std;
#define mp make_pair
#define pb push_back
#define INF 10000
int n,m,x;
string s[20];
int res;
int getconfig(int row)
{
int ret=0,cnt=0;
for(int i=n-1;i>=0;i--)
{
if(s[row][i]=='.')
ret=ret|(1<<cnt);
cnt++;
}
return ret;
}
void calc(int row,int config,int setbits,int total)
{
if(row==m)
{
if(config==(int)((1<<n)-1))
res=min(res,total);
return;
}
int cur=getconfig(row)^setbits;
int temp=config,cnt=0,nextset=0;
for(int i=0;i<n;i++)
{
if(!(config&(1<<i)))
{
if(i==0)
cur=cur^(3<<i);
else if(i==n-1)
cur=cur^(3<<(n-2));
else
cur=cur^(7<<(i-1));
cnt++;
nextset=nextset|(1<<i);
}
}
cur=cur&((1<<n)-1);
calc(row+1,cur,nextset,total+cnt);
}
void build()
{
int config=getconfig(0);
calc(1,config,0,0);
for(int i=1;i<(1<<n);i++)
{
int store=config,temp=i,cnt=0;
for(int j=0;j<n;j++)
{
if(temp&(1<<j))
{
cnt++;
if(j==0)
store=store^3;
else if(j==n-1)
store=store^(3<<(n-2));
else
store=store^(7<<(j-1));
}
}
store=store&((1<<n)-1);
calc(1,store,i,cnt);
}
}
int main()
{
//freopen("input.txt","r",stdin);
while(1)
{
res=INF;
cin>>m>>n;
x=n+1;
if(n==0 && m==0)
break;
for(int i=0;i<m;i++)
cin>>s[i];
build();
if(res==INF)
cout<<"Damaged billboard.\n";
else
cout<<"You have to tap "<<res<<" tiles.\n";
}
}