搜索类题目
#include<stdio.h>
typedef struct
{
int up,right,down,left;
int amount;
}SQ;
SQ SQUARE[25],KIND[25],tempSQUARE[25];
int N,AMOUNT,COLOR[25],JUMP,kindAMOUNT,TOP;
int switchSQUARE(int k)
{
return SQUARE[k].up+SQUARE[k].right*10
+SQUARE[k].down*100+SQUARE[k].left*1000;
}
void sortSQUARE()
{
int i,j;
SQ temp;
for(i=0;i<=AMOUNT-1;i++)
{
for(j=AMOUNT-1;j>i;j--)
{
if(switchSQUARE(j-1)>switchSQUARE(j))
{
temp=SQUARE[j];
SQUARE[j]=SQUARE[j-1];
SQUARE[j-1]=temp;
}
}
}
for(i=j=0;i<AMOUNT;i++)
{
KIND[j]=SQUARE[i];
KIND[j].amount=1;
while(i<AMOUNT-1&&switchSQUARE(i)==switchSQUARE(i+1))
{
KIND[j].amount++;
i++;
}
j++;
}
kindAMOUNT=j;
}
int judgeCASE(int L)
{
if(L/N>0)
if(tempSQUARE[L].up!=tempSQUARE[L-N].down)
return 0;
if(L%N>0)
if(tempSQUARE[L].left!=tempSQUARE[L-1].right)
return 0;
return 1;
}
void DFS(int L)
{
int k;
if(L>=AMOUNT)
{
JUMP=1;
return;
}
for(k=0;k<kindAMOUNT;k++)
{
if(KIND[k].amount==0) continue;
tempSQUARE[L]=KIND[k];
if(judgeCASE(L)==1)
{
TOP++;
KIND[k].amount--;
DFS(L+1);
if(JUMP==1) return;
TOP--;
KIND[k].amount++;
}
}
}
int main()
{
int i,count=0;
while(scanf("%d",&N)&&N)
{
AMOUNT=N*N;
JUMP=0;
TOP=0;
count++;
for(i=0;i<AMOUNT;i++)
{
scanf("%d%d%d%d",&SQUARE[i].up,&SQUARE[i].right,
&SQUARE[i].down,&SQUARE[i].left);
}
sortSQUARE();
DFS(0);
if(count!=1) printf("\n");
if(TOP>=AMOUNT-1)
printf("Game %d: Possible\n", count);
else
printf("Game %d: Impossible\n", count);
}
return 0;
}