Uva 1587:Box

题目传送门:https://cn.vjudge.net/problem/UVA-1587

#include <stdio.h>

struct p
{
    int w, h;
}ps[6], cps[3];

bool isSame(int i, int j)
{
    return (ps[i].w == ps[j].w && ps[i].h == ps[j].h)
        || (ps[i].w == ps[j].h && ps[i].h == ps[j].w);
}

bool dfs(int a, int b, int depth) // 旋转拼接三个板子
{
    if (2 == depth) {
        if (a == cps[depth].w && b == cps[depth].h) return true;
        if (a == cps[depth].h && b == cps[depth].w) return true;
        return false;
    }
    if (cps[depth].w == a)
        if (dfs(cps[depth].h, b, depth+1))
            return true;
    if (cps[depth].h == a)
        if (dfs(cps[depth].w, b, depth+1))
            return true;
    return false;
}

int main()
{
    while (2 == scanf("%d%d", &ps[0].w, &ps[0].h)) {
        bool used[6] = {false}; int ct = 0;
        for (int i = 1; i < 6; ++i)
            scanf("%d%d", &ps[i].w, &ps[i].h);

        for (int i = 0, j; i < 6; ++i) { // 首先确定是否有三对相同的板子
            if (used[i]) continue;
            for (j = i + 1; j < 6; ++j) {
                if (used[j]) continue;
                if (isSame(i, j)) {
                    used[i] = used[j] = true;
                    cps[ct++] = ps[i]; // 记住三组板子
                    break;
                }
            }
            if (6 == j)
                break;
        }
        //printf("ct = %d\n", ct);
        if (ct < 3) { printf("IMPOSSIBLE\n"); continue; }

        if (dfs(cps[0].w, cps[0].h, 1) || dfs(cps[0].h, cps[0].w, 1)) // 旋转拼接三个板子
            printf("POSSIBLE\n");
        else
            printf("IMPOSSIBLE\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值