UV 12113 Overlapping Squares, 12113 overlapping
Question:
There are 6 2*2 squares in total to determine whether the given shape can be formed.
Ideas:
A square has a total of 9 placement methods, and a total of 2 9 Power Placement methods for the whole map. Then, the map is represented by an array of 9*5, and the position of the square is represented by the subscript of its eight sides and four hollow subscript.
Code:
# Include <iostream>
# Include <cstring>
# Include <cstdio>
# Include <algorithm>
Using namespace std;
Int bian [9] [8] = {
},
},
},
},
},
{, 32 },
{19,21, 27,31, 36,37, 39,40 },
{, 23, 39 },
{, 35, 41,}; // edge coordinates of 9 placement methods of 2*2 square
Int nei [9] [4] = {
10, 11, 12, 20,
12,13, 14,22,
14,15, 16,24,
19,20, 21,29,
, 22,
, 33,
28, 29, 30, 38,
30, 31, 32, 40,
32, 33, 34, 42}; // null coordinates of 9 placement methods of 2*2 square
Char str [100];
Int map [45], cot [10], map1 [45];
Int read () // read data
{
Int h = 0, cnt = 0, kk = 0;
For (int I = 0; I <5; I ++)
{
If (gets (str) = NULL)
Return-1;
If (str [0] = '0 ')
Return-1;
For (int j = 0; j <9; j ++)
{
Map [kk ++] = str [j] = 32? ;
If (str [j]! = 32)
Cnt ++;
}
}
Return cnt;
}
Int count (int s) // calculates the total number of Squares used
{
Return s = 0? 0: count (s/2) + (s & 1 );
}
Void stick (int p, int & cnt) // construct the map after the p-th square is pasted.
{
Int I, j;
For (I = 0; I <8; I ++)
{
If (map1 [bian [p] [I] = 0) // if there is no edge at the side of the p-th Square, the number of edges + 1
Cnt ++;
Map1 [bian [p] [I] = 1;
}
For (I = 0; I <4; I ++)
{
If (map1 [nei [p] [I] = 1) // Number of edges in the hollow position of the p-th square if an edge exists-1
cnt--;
map1[nei[p][i]]=0;
}
}
int main()
{
int flag,cnt;
int i,j,k;
int h=1;
while((flag=read())&&flag!=-1)
{
//cout<<flag<<endl;
int flag1=0;
for(i=0;i<(1<<9);i++)
{
int n=count(i);
if(n>6||8*n<flag)
continue;
k=0;
for(j=0;j<9;j++)
if((i>>j)&1)
cot[k++]=j;
//cout<<k<<endl;
do
{
//cout<<1<<endl;
memset(map1,0,sizeof(map1));
cnt=0;
//cout<<n<<endl;
for(j=0;j<n;j++)
{
int p=cot[j];//cout<<1<<endl;
stick(p,cnt);
}
if(cnt==flag)
{
int flag2=1;
for(int l=0;l<45;l++)
{
if(map[l]!=map1[l])
{
flag2=0;break;
}
}if(flag2)
flag1=1;
}
if(flag1)
break;
}
while(next_permutation(cot,cot+k));
}
printf("Case %d: %s\n",h++, flag1? "Yes" : "No");
}
return 0;
}