这道题思路不难,但是写起来有点麻烦。
我们先遍历6块板子,看能不能找到3对一样的板子,如果找不到,就直接IMPOSSIBLE了,如果能找到再继续判断能不能构成长方体。
至于判断能不能组成长方体,我们可以固定一个板子不动,这里我们固定第一个板子。然后再拿其他板子和第一个接上,如果接不上,
可以把这个板子旋转90° ,然后继续拿第三个板子,做同样的事情。
另外,固定的板子要分从长的一边开始拼接和从短的一边开始拼接。
新手的代码还是挺长的:
-
#include <stdio.h>
-
#include <string.h>
-
-
int isUsed[6] = {0, 0, 0, 0, 0, 0};
-
struct p
-
{
-
int x;
-
int y;
-
} palet[6], cupple[3];
-
-
int dfs(int l, int m, int depth);
-
-
int main()
-
{
-
int i, j, w, h, q;
-
while(scanf("%d%d", &palet[0].x, &palet[0].y) == 2)
-
{
-
int flag = 0;
-
memset(cupple, 0, sizeof(cupple));
-
memset(isUsed, 0, sizeof(isUsed));
-
for(i = 1; i < 6; ++i)
-
scanf("%d%d", &palet[i].x, &palet[i].y);
-
for(i = 0, q = 0; i < 6; ++i)
-
{
-
if(isUsed[i]) continue;
-
w = palet[i].x;
-
h = palet[i].y;
-
for(j = i + 1; j < 6; ++j)
-
{
-
if((w == palet[j].x && h == palet[j].y) || h == palet[j].x && w == palet[j].y)
-
{
-
isUsed[i] = isUsed[j] = 1; //找到了就标记为用过了
-
cupple[q].x = w;
-
cupple[q].y = h;
-
++q;
-
break;
-
}
-
}
-
//如果找不到两个相同的就跳出,肯定不行
-
if(6 == j)
-
{
-
flag = 1;
-
break;
-
}
-
}
-
if(flag)
-
{
-
printf("IMPOSSIBLE\n");
-
continue;
-
}
-
-
//将第一个板子固定,然后旋转其他三个来判断是否可以构成长方体
-
if(dfs(cupple[0].x, cupple[0].y, 1) || dfs(cupple[0].y, cupple[0].x, 1)) printf("POSSIBLE\n");
-
else printf("IMPOSSIBLE\n");
-
}
-
return 0;
-
}
-
-
//判断是否可以构成长方体
-
int dfs(int l, int m, int depth)
-
{
-
if(2 == depth)
-
{
-
if(l == cupple[depth].x && cupple[depth].y == m) return 1;
-
if(l == cupple[depth].y && cupple[depth].x == m) return 1;
-
return 0;
-
}
-
-
if(l == cupple[depth].x)
-
{
-
if(dfs(cupple[depth].y, m, depth + 1))
-
return 1;
-
}
-
else if(l == cupple[depth].y)
-
{
-
if(dfs(cupple[depth].x, m, depth + 1))
-
return 1;
-
}
-
return 0;
-
-
}
阅读(1460) | 评论(0) | 转发(0) |