-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathFreeform Factory.cpp
89 lines (76 loc) · 2.05 KB
/
Freeform Factory.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
# include <bits/stdc++.h>
# define INF 123123123
# define NR 1005
using namespace std;
ifstream f("date.in");
ofstream g("date.out");
int i,j,n,m,T,cost;
int sol,X[NR],V[NR],aux[NR][NR];
char M[NR][NR];
void BACK (int k) {
if (k==n*n+1) {
// cum verific ?
for (int i=1; i<=n; ++i) {
int VV=0;
for (int j=1; j<=n; ++j) {
if (aux[i][j]==1) V[++VV]=j;
}
if (VV==0) return;
int Q=0;
for (int j=1; j<=n; ++j)
if (j!=i) X[++Q]=j;
X[Q+1]=0; X[Q+2]=0; X[Q+3]=0; X[Q+4]=0;
// acum verific
if (VV==1) {
if (aux[X[1]][V[1]] || aux[X[2]][V[1]] || aux[X[3]][V[1]]) return;
}
if (VV==2) {
if ((aux[X[1]][V[1]] || aux[X[2]][V[1]] || aux[X[3]][V[1]]) &&
(aux[X[1]][V[2]] || aux[X[2]][V[2]] || aux[X[3]][V[2]]))
return;
}
if (VV==3) {
if ((aux[X[1]][V[1]] || aux[X[2]][V[1]] || aux[X[3]][V[1]]) &&
(aux[X[1]][V[2]] || aux[X[2]][V[2]] || aux[X[3]][V[2]]) &&
(aux[X[1]][V[3]] || aux[X[2]][V[3]] || aux[X[3]][V[3]]))
return;
}
}
if (cost < sol) sol=cost;
} else {
int L=(k-1)/n+1;
int C=(k-1)%n+1;
if (M[L][C]=='1') {
aux[L][C]=1;
BACK(k+1);
aux[L][C]=0;
} else {
//il iau
aux[L][C]=1; ++cost;
BACK(k+1);
aux[L][C]=0;
// nu il iau
aux[L][C]=0;
BACK(k+1);
}
}
}
void init() {
for (int i=1; i<=n; ++i)
for (int j=1; j<=n; ++j)
aux[i][j]=0;
}
int main ()
{
f>>T; int test=0;
while (T--) {
++test; g<<"Case #"<<test<<": ";
f>>n; f.get(); init();
for (int i=1; i<=n; ++i)
f.getline(M[i]+1, NR);
sol=INF; cost=0;
BACK (1);
g<<sol<<"\n";
}
return 0;
}