Chinaunix首页 | 论坛 | 博客
  • 博客访问: 77044
  • 博文数量: 32
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 284
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-26 14:00
个人简介

有梦想的人,正在努力

文章分类

全部博文(32)

文章存档

2015年(32)

我的朋友

分类: C/C++

2015-06-08 13:11:44

这道题思路不难,但是写起来有点麻烦。
我们先遍历6块板子,看能不能找到3对一样的板子,如果找不到,就直接IMPOSSIBLE了,如果能找到再继续判断能不能构成长方体。
至于判断能不能组成长方体,我们可以固定一个板子不动,这里我们固定第一个板子。然后再拿其他板子和第一个接上,如果接不上,
可以把这个板子旋转90° ,然后继续拿第三个板子,做同样的事情。

另外,固定的板子要分从长的一边开始拼接和从短的一边开始拼接。
新手的代码还是挺长的:

  1. #include <stdio.h>
  2. #include <string.h>

  3. int isUsed[6] = {0, 0, 0, 0, 0, 0};
  4. struct p
  5. {
  6.     int x;
  7.     int y;
  8. } palet[6], cupple[3];

  9. int dfs(int l, int m, int depth);

  10. int main()
  11. {
  12.     int i, j, w, h, q;
  13.     while(scanf("%d%d", &palet[0].x, &palet[0].y) == 2)
  14.     {
  15.         int flag = 0;
  16.         memset(cupple, 0, sizeof(cupple));
  17.         memset(isUsed, 0, sizeof(isUsed));
  18.         for(i = 1; i < 6; ++i)
  19.             scanf("%d%d", &palet[i].x, &palet[i].y);
  20.         for(i = 0, q = 0; i < 6; ++i)
  21.         {
  22.             if(isUsed[i]) continue;
  23.             w = palet[i].x;
  24.             h = palet[i].y;
  25.             for(j = i + 1; j < 6; ++j)
  26.             {
  27.                 if((w == palet[j].x && h == palet[j].y) || h == palet[j].x && w == palet[j].y)
  28.                 {
  29.                     isUsed[i] = isUsed[j] = 1; //找到了就标记为用过了
  30.                     cupple[q].x = w;
  31.                     cupple[q].y = h;
  32.                     ++q;
  33.                     break;
  34.                 }
  35.             }
  36.             //如果找不到两个相同的就跳出,肯定不行
  37.             if(6 == j)
  38.             {
  39.                 flag = 1;
  40.                 break;
  41.             }
  42.         }
  43.         if(flag)
  44.         {
  45.             printf("IMPOSSIBLE\n");
  46.             continue;
  47.         }

  48.         //将第一个板子固定,然后旋转其他三个来判断是否可以构成长方体
  49.         if(dfs(cupple[0].x, cupple[0].y, 1) || dfs(cupple[0].y, cupple[0].x, 1)) printf("POSSIBLE\n");
  50.         else printf("IMPOSSIBLE\n");
  51.     }
  52.     return 0;
  53. }

  54. //判断是否可以构成长方体
  55. int dfs(int l, int m, int depth)
  56. {
  57.     if(2 == depth)
  58.     {
  59.         if(l == cupple[depth].x && cupple[depth].y == m) return 1;
  60.         if(l == cupple[depth].y && cupple[depth].x == m) return 1;
  61.         return 0;
  62.     }

  63.     if(l == cupple[depth].x)
  64.     {
  65.         if(dfs(cupple[depth].y, m, depth + 1))
  66.             return 1;
  67.     }
  68.     else if(l == cupple[depth].y)
  69.     {
  70.         if(dfs(cupple[depth].x, m, depth + 1))
  71.             return 1;
  72.     }
  73.     return 0;

  74. }

阅读(1460) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~