Test instructions
There are a total of 6 square 2*2, judging whether it can be the given shape.
Ideas:
A square total of 9 kinds of placement, for the whole map to put a total of 2 of the 9-way placement. The map is then represented by an array of 9*5, and the position of the square is represented by its 8-edged subscript and 4 hollow subscripts.
Code:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace Std;
int bian[9][8]={
{1,3,9,13,18,19,21,22},
{3,5,11,15,20,21,23,24},
{5,7,13,17,22,23,25,26},
{10,12,18,22,27,28,30,31},
{12,14,20,24,29,30,32,33},
{14,16,22,26,31,32,34,35},
{19,21,27,31,36,37,39,40},
{21,23,29,33,38,39,41,42},
{23,25,31,35,40,41,43,44}};//2*2 Square's 9-way side coordinates
int nei[9][4]={
10,11,12,20,
12,13,14,22,
14,15,16,24,
19,20,21,29,
21,22,23,31,
23,24,25,33,
28,29,30,38,
30,31,32,40,
The hollow coordinates of 9 kinds of 32,33,34,42};//2*2 squares
Char str[100];
int map[45],cot[10],map1[45];
Int Read ()//read-in 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?0:1;
if (str[j]! =)
cnt++;
}
}
return cnt;
}
int count (int s)//calculation uses a total of several squares
{
return S==0?0:count (S/2) + (s&1);
}
void Stick (int p,int &cnt)//build the map after the first p squares are pasted
{
int i,j;
for (i=0;i<8;i++)
{
if (map1[bian[p][i]]==0)//The position of the P-square edge if there is no edge, the number of edges +1
cnt++;
Map1[bian[p][i]]=1;
}
for (i=0;i<4;i++)
{
if (map1[nei[p][i]]==1) //P-Square Hollow If there is an edge, the number of sides-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<<en dl
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;
}
UVA 12113 Overlapping Squares