http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008
check() 如果在k不是在最左边就水平两个三角形比较。不是在最上边就垂直两个三角形比较。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
int a[30][4];
int b[25],c[25];//c类型数,b记录当前放置的格子下标。
int type,n,flag;
bool check(int k)
{
if(k>=n&&a[b[k]][0]!=a[b[k-n]][2])
return false;
if(k%n!=0&&a[b[k]][3]!=a[b[k-1]][1])
return false;
return true;
}
void dfs(int x)
{
if(x == n*n)
{
flag = 1;
return ;
}
for(int i=0;i<type;i++)
{
if(flag == 1)
return ;
if(c[i]>0)
{
c[i]--;
b[x]=i;
if(check(x))
dfs(x+1);
c[i]++;
}
}
}
int main()
{
int count=0;
int top,right,left,bottom;
while(cin>>n)
{
type=flag=0;
if(n==0)
break;
count++;
memset(c,0,sizeof(c));
memset(b,0,sizeof(b));
for(int i=0;i<n*n;i++)
{
cin>>top>>right>>bottom>>left;
int j=0;
while(j<type)
{
if(a[j][0]==top&&a[j][1]==right&&a[j][2]==bottom&&a[j][3]==left)
{
c[j]++;
break;
}
j++;
}
if(j == type)
{
a[type][0] = top;
a[type][1] = right;
a[type][2] = bottom;
a[type][3] = left;
c[type]=1;
type++;
}
}
dfs(0);
if(count>1)
cout<<endl;
if(flag == 1)
cout<<"Game "<<count<<": Possible"<<endl;
else
cout<<"Game "<<count<<": Impossible"<<endl;
}
}