zju 1008 Gnome Tetravex dfs

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;

    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值