题目传送门: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;
}