Test instructions
Given a grid of matches, and then remove some of the matches, ask at least a few more matches to get a square in the picture.
Ideas:
1. Due to the range of n given in the title, 2 * n * (n + 1) <= so that all matches are represented by a long long bit operation;
2. The calculation of heuristic function H needs to be considered: if one side of a square is deleted, then H (S1) <= h (S2) + C (S1, S2) is guaranteed, where C (S1, s2) = 1,h (S1)-H (S2) <= 1;
3. The range of the various bit operations to be clear, such as 1<<i before a long long modifier to get the correct results
Code:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace Std;
const int infs = 0x7fffffff;
int n,c,e,bound;
Long Long squ[100],xiao[6][6];
BOOL Flag;
Long long Get2 (int i)
{
Return (long Long) 1<< (i-1));
}
int getnumber1 (int i,int j)
{
Return (2*n+1) * (i-1) +j;
}
int getnumber2 (int i,int j)
{
Return (2*n+1) * (i-1) +j+n;
}
void Build ()
{
c=0;
memset (Xiao,0,sizeof (Xiao));
int i,j;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
Xiao[i][j]|=get2 (Getnumber1 (i,j)) |get2 (Getnumber1 (i+1,j));
Xiao[i][j]|=get2 (Getnumber2 (i,j)) |get2 (Getnumber2 (i,j+1));
SQU[C++]=XIAO[I][J];
}
for (int size=2;size<=n;size++)
{
for (i=1;i+size-1<=n;i++)
{
for (j=1;j+size-1<=n;j++)
{
squ[c]=0;
for (int a=0;a<size;a++)
{
for (int b=0;b<size;b++)
{
SQU[C]^=XIAO[I+A][J+B];;
}
}
C + +;
}
}
}
}
int Dfs (long long state,int de)
{
int h=0;
Long Long u=0,s=state;
for (int i=0;i<c;i++)
{
if ((S&squ[i]) ==squ[i])
{
H+=1;
S^=squ[i];
if (u==0)
U=squ[i];
}
}
if (h==0)
{
Flag=true;
Return de;
}
if (De+h>bound)
return de+h;
int News=infs;
for (int i=1;i<=e;i++)
{
if (U&get2 (i))
{
int B=dfs (State^get2 (i), de+1);
if (flag)
return b;
News=min (b,news);
}
}
return news;
}
int main ()
{
int t;
scanf ("%d", &t);
while (t--)
{
scanf ("%d", &n);
Build ();
e=2*n* (n+1);
int k;
Long Long state= (long Long) 1<<e)-1;
scanf ("%d", &k);
for (int i=0;i<k;i++)
{
int x;
scanf ("%d", &x);
State^=get2 (x);
}
Flag=false;
Bound=0;
while (!flag)
{
Bound=dfs (state,0);
}
printf ("%d\n", bound);
}
}
UVA 1603 Square Destroyer