题意:给你28个多米勒牌,要求刚好铺满一个7x8的图,输出所有答

案。每个牌只能使用一次

思路:

对每个位置分别搜索其右边 和 下边。

但是在中途,细节上有点问题。最开始想的是搜到最后一个点输出答案,但总是有问题。然后搜索部分换了个姿势,记录以使用的牌数,终于AC。感觉 - -自己好坑

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <queue>
  6. #include <algorithm>
  7. typedef long long ll;
  8. using namespace std;
  9. const int inf = 0x3f3f3f3f;
  10. int all;
  11.  
  12. int vis[10][10];
  13. int ans[10][10];
  14. int p[10][10];
  15. int pip[10][10];
  16. int vis_pip[30][30];
  17. int dir[2][2] = {{0,1},{1,0}};
  18.  
  19. void ini()
  20. {
  21. int tt = 1;
  22. for(int i = 0; i < 7; i++)
  23. for(int j = i; j < 7; j++)
  24. {
  25. pip[i][j] = pip[j][i] = tt++;
  26. }
  27. return;
  28. }
  29.  
  30. void dfs(int cur,int num)
  31. {
  32.  
  33. int x = cur/8;
  34. int y = cur%8;
  35. if(num == 28) //搜满28个
  36. {
  37. for(int i = 0; i < 7; i++)
  38. {
  39. for(int j = 0; j < 8; j++)
  40. {
  41. printf("%4d",ans[i][j]);
  42. }
  43. printf("\n");
  44. }
  45. all ++;
  46. printf("\n");
  47. return ;
  48. }
  49. if(vis[x][y])
  50. {
  51. dfs(cur+1,num);
  52. return ;
  53. }
  54. if(x > 6 || y > 7)
  55. return ;
  56. for(int i = 0;i < 2;i++)
  57. {
  58. if(i == 0 && y == 7) continue;
  59. if(i == 1 && x == 6) continue;
  60. int tx = x + dir[i][0];
  61. int ty = y + dir[i][1];
  62. if(vis[tx][ty] || vis_pip[p[x][y]][p[tx][ty]])
  63. continue;
  64. vis[x][y] = vis[tx][ty]= vis_pip[p[x][y]][p[tx][ty]] = vis_pip[p[tx][ty]][p[x][y]] = 1;
  65. ans[x][y] = ans[tx][ty] = pip[p[x][y]][p[tx][ty]];
  66. dfs(cur+1,num+1);
  67. vis[x][y] = vis[tx][ty]= vis_pip[p[x][y]][p[tx][ty]] = vis_pip[p[tx][ty]][p[x][y]] = 0;
  68. }
  69. return ;
  70. }
  71.  
  72. int main()
  73. {
  74. int cas = 1;
  75. ini();
  76. // freopen("in.txt","r",stdin);
  77. while(scanf("%d%d%d%d%d%d%d%d",&p[0][0],&p[0][1],&p[0][2],&p[0][3],&p[0][4],&p[0][5],&p[0][6],&p[0][7]) != EOF)
  78. {
  79. for(int i = 1; i < 7; i++)
  80. for(int j = 0; j < 8; j++)
  81. {
  82. scanf("%d",&p[i][j]);
  83. }
  84. if(cas!= 1)
  85. printf("\n\n\n");
  86. memset(vis,0,sizeof(vis));
  87. memset(vis_pip,0,sizeof(vis_pip));
  88. printf("Layout #%d:\n\n",cas);
  89. for(int i = 0; i < 7; i++)
  90. {
  91. for(int j = 0; j <8; j++)
  92. {
  93. printf("%4d",p[i][j]);
  94. }
  95. printf("\n");
  96. }
  97. printf("\n");
  98. printf("Maps resulting from layout #%d are:\n\n",cas);
  99. all = 0;
  100.  
  101. dfs(0,0);
  102. printf("There are %d solution(s) for layout #%d.\n",all,cas++);
  103. }
  104. return 0;
  105. }

  

习题 7-3 uva211的更多相关文章

  1. Sharepoint学习笔记—习题系列--70-576习题解析 --索引目录

        Sharepoint学习笔记—习题系列--70-576习题解析  为便于查阅,这里整理并列出了70-576习题解析系列的所有问题,有些内容可能会在以后更新. 需要事先申明的是:     1. ...

  2. 《python核心编》程课后习题——第三章

    核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...

  3. 习题 5: 更多的变量和打印 | 笨办法学 Python

    一. 简述 “格式化字符串(format string)” -  每一次你使用 ' ’ 或 " " 把一些文本引用起来,你就建立了一个字符串. 字符串是程序将信息展示给人的方式. ...

  4. 【WebGoat习题解析】Parameter Tampering->Bypass HTML Field Restrictions

    The form below uses HTML form field restrictions. In order to pass this lesson, submit the form with ...

  5. python核心编程(第二版)习题

    重新再看一遍python核心编程,把后面的习题都做一下.

  6. SQL简单语句总结习题

    创建一个表记员工个人信息: --创建一个表 create table plspl_company_info( empno ) not null, ename ) not null, job ), ma ...

  7. 《Python核心编程》部分代码习题实践(持续更新)

    第三章 3-10 交换异常处理方式 代码: #makeTextFile.py #!/usr/bin/env python 'makeTextFile.py' import os ls = os.lin ...

  8. web实验指导书和课后习题参考答案

    实验指导书 :http://course.baidu.com/view/daf55bd026fff705cc170add.html 课后习题参考答案:http://wenku.baidu.com/li ...

  9. 《C++primer》v5 第1章 开始 读书笔记 习题答案

    从今天开始在博客里写C++primer的文字.主要以后面的习题作业为主,会有必要的知识点补充. 本人也是菜鸟,可能有不对之处,还望指出. 前期内容可能会比较水. 1.1略 1.2略 1.3 cin和c ...

随机推荐

  1. JavaScript 相关知识

    一.数组   var a = [1,2,3,4]; console.log(a.length); a.push(5); console.log(a); // [1, 2, 3, 4, 5] var r ...

  2. 深入分析Java Web中的编码问题

    编码问题一直困扰着我,每次遇到乱码或者编码问题,网上一查,问题解决了,但是实际的原理并没有搞懂,每次遇到,都是什么头疼. 决定彻彻底底的一次性解决编码问题. 1.为什么要编码 计算机的基本单元是字节, ...

  3. Tomcat 8项目无法启动,无报错

    作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Tomcat 8启动很慢,且日志上无任何错误,在日志中查看到如下信息: Log4j:[2015-10-29 ...

  4. Web Api 利用 cors 实现跨域

    一.安装 cors 二.修改 Web.config <appSettings> <add key="cors:allowedMethods" value=&quo ...

  5. Docker学习笔记 - Docker的镜像

    一个容器实际上是运行在宿主机上的一个进程. 只不过在启动这个进程之前进行了一些特殊处理,让这个容器进入了一个全新的虚拟环境,与宿主机的环境分开, 所以这个进程及其子进程认为自己运行在一个独立的世界里面 ...

  6. Oracle 存储过程简单语法

    一.无参数的存储过程 --创建存储过程create or replace procedure getdate as datetime varchar2(); begin select to_char( ...

  7. NHibernate优点和缺点:

    NHibernate优点: 1.完全的ORM框架. NHibernate对数据库结构提供了较为完整的封装,它将数据库模式映射为较完全的对象模型,支持封装,继续机制,功能较强大,比一般的ORM灵活性高. ...

  8. 十个你需要在 PHP 7 中避免的坑

    1. 不要使用 mysql_ 类函数 终于,你不用再看到建议不要使用 mysql_ 函数的提示了.因为 PHP 7 从核心上完全移除了它们,这意味着请你移步至更好的 mysqli_ 类函数,或者更灵活 ...

  9. Mac里安装配置Jdk

    #下载jdk7的mac版 #官网下载地址http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.h ...

  10. packer的基本使用

    工具的产生,一定是为了解决某些痛点,那么痛点是? 你们在工作中是不是经常用到各种云?aliyun, aws, digitalOcean and so on? 你们的规模不大不小,经常去云平台上点一点, ...