习题 7-3 uva211
题意:给你28个多米勒牌,要求刚好铺满一个7x8的图,输出所有答
案。每个牌只能使用一次
思路:
对每个位置分别搜索其右边 和 下边。
但是在中途,细节上有点问题。最开始想的是搜到最后一个点输出答案,但总是有问题。然后搜索部分换了个姿势,记录以使用的牌数,终于AC。感觉 - -自己好坑
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <algorithm>
typedef long long ll;
using namespace std;
const int inf = 0x3f3f3f3f;
int all; int vis[10][10];
int ans[10][10];
int p[10][10];
int pip[10][10];
int vis_pip[30][30];
int dir[2][2] = {{0,1},{1,0}}; void ini()
{
int tt = 1;
for(int i = 0; i < 7; i++)
for(int j = i; j < 7; j++)
{
pip[i][j] = pip[j][i] = tt++;
}
return;
} void dfs(int cur,int num)
{ int x = cur/8;
int y = cur%8;
if(num == 28) //搜满28个
{
for(int i = 0; i < 7; i++)
{
for(int j = 0; j < 8; j++)
{
printf("%4d",ans[i][j]);
}
printf("\n");
}
all ++;
printf("\n");
return ;
}
if(vis[x][y])
{
dfs(cur+1,num);
return ;
}
if(x > 6 || y > 7)
return ;
for(int i = 0;i < 2;i++)
{
if(i == 0 && y == 7) continue;
if(i == 1 && x == 6) continue;
int tx = x + dir[i][0];
int ty = y + dir[i][1];
if(vis[tx][ty] || vis_pip[p[x][y]][p[tx][ty]])
continue;
vis[x][y] = vis[tx][ty]= vis_pip[p[x][y]][p[tx][ty]] = vis_pip[p[tx][ty]][p[x][y]] = 1;
ans[x][y] = ans[tx][ty] = pip[p[x][y]][p[tx][ty]];
dfs(cur+1,num+1);
vis[x][y] = vis[tx][ty]= vis_pip[p[x][y]][p[tx][ty]] = vis_pip[p[tx][ty]][p[x][y]] = 0;
}
return ;
} int main()
{
int cas = 1;
ini();
// freopen("in.txt","r",stdin);
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)
{
for(int i = 1; i < 7; i++)
for(int j = 0; j < 8; j++)
{
scanf("%d",&p[i][j]);
}
if(cas!= 1)
printf("\n\n\n");
memset(vis,0,sizeof(vis));
memset(vis_pip,0,sizeof(vis_pip));
printf("Layout #%d:\n\n",cas);
for(int i = 0; i < 7; i++)
{
for(int j = 0; j <8; j++)
{
printf("%4d",p[i][j]);
}
printf("\n");
}
printf("\n");
printf("Maps resulting from layout #%d are:\n\n",cas);
all = 0; dfs(0,0);
printf("There are %d solution(s) for layout #%d.\n",all,cas++);
}
return 0;
}
习题 7-3 uva211的更多相关文章
- Sharepoint学习笔记—习题系列--70-576习题解析 --索引目录
Sharepoint学习笔记—习题系列--70-576习题解析 为便于查阅,这里整理并列出了70-576习题解析系列的所有问题,有些内容可能会在以后更新. 需要事先申明的是: 1. ...
- 《python核心编》程课后习题——第三章
核心编程课后习题——第三章 3-1 由于Python是动态的,解释性的语言,对象的类型和内存都是运行时确定的,所以无需再使用之前对变量名和变量类型进行申明 3-2原因同上,Python的类型检查是在运 ...
- 习题 5: 更多的变量和打印 | 笨办法学 Python
一. 简述 “格式化字符串(format string)” - 每一次你使用 ' ’ 或 " " 把一些文本引用起来,你就建立了一个字符串. 字符串是程序将信息展示给人的方式. ...
- 【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 ...
- python核心编程(第二版)习题
重新再看一遍python核心编程,把后面的习题都做一下.
- SQL简单语句总结习题
创建一个表记员工个人信息: --创建一个表 create table plspl_company_info( empno ) not null, ename ) not null, job ), ma ...
- 《Python核心编程》部分代码习题实践(持续更新)
第三章 3-10 交换异常处理方式 代码: #makeTextFile.py #!/usr/bin/env python 'makeTextFile.py' import os ls = os.lin ...
- web实验指导书和课后习题参考答案
实验指导书 :http://course.baidu.com/view/daf55bd026fff705cc170add.html 课后习题参考答案:http://wenku.baidu.com/li ...
- 《C++primer》v5 第1章 开始 读书笔记 习题答案
从今天开始在博客里写C++primer的文字.主要以后面的习题作业为主,会有必要的知识点补充. 本人也是菜鸟,可能有不对之处,还望指出. 前期内容可能会比较水. 1.1略 1.2略 1.3 cin和c ...
随机推荐
- mysql5.5中datetime默认值不能为NOW或者CURRENT_TIMESTAMP,用触发器解决
mysql5.6及以上的版本datatime默认值可以为CURRENT_TIMESTAMP或者NOW 那我们要用的是mysql5.5及以下版本呢? 请看代码 delimiter // DROP TRI ...
- js定时刷新页面.
//页面定时刷新.2017.09.27 $(document).ready(function () { self.setInterval(function () { var d = new Date( ...
- java 实现多文件打包下载
jsp页面js代码: function downloadAttached(){ var id = []; id.push(infoid); var options = {}; options.acti ...
- 新概念英语(1-1)Excuse me!
Excuse me!Whose handbag is it? A:Excuse me! B:Yes? A:Is this your handbag? B:Pardon? A:Is this your ...
- setInterval()使用时易疏忽的点
举个例子: 一道题目 这两个程序的区别就在于我向setInterval的参数一function写入了参数.这就是导致运行结果不尽如人意的原因. setInterval()方法可以接收三个参数,此参数会 ...
- python实现:最长子字符串
给定一个字符串 s 和正整数 n,请使用你熟悉的编程语言输出 s 中包含不超过 n 种字符的最长子串,如 s="uabbcadbaef",n=4 时应该输出 "abbca ...
- webpack打包性能优化
1. 使用 gzip 压缩打包后的 js 文件 这个方法优化浏览器下载时的文件大小(打包后的文件大小没有改变) webpack.config.prod.js 中 var CompressionWebp ...
- HTNL表单详解
HTML表单 表单的结构 表单的标签:<form> </form> 常用属性 Name , method(get,post), action(服务器的接收的页面如:reg.ph ...
- DataContractJsonSerializer序列化时间类型时转换为UTC溢出问题
问题描述 如下一个实体类,含有非空时间类型属性,默认是C#的最小时间,在使用DataContractJsonSerializer将该类对象序列化成JSON时,抛出异常信息:System.Runtime ...
- 框架学习之Struts2(二)---基本配置和封装表单数据
一.结果页面配置 1.局部结果页面配置 <!-- 局部结果页面配置--> <package name = "demo" extends = "strut ...