uva 211(dfs)
211 - The Domino Effect
Time limit: 3.000 seconds
A standard set of Double Six dominoes contains 28 pieces (called bones) each displaying two numbers
from 0 (blank) to 6 using dice-like pips. The 28 bones, which are unique, consist of the following
combinations of pips:
Bone # Pips Bone # Pips Bone # Pips Bone # Pips
1 0 | 0 8 1 | 1 15 2 | 3 22 3 | 6
2 0 | 1 9 1 | 2 16 2 | 4 23 4 | 4
3 0 | 2 10 1 | 3 17 2 | 5 24 4 | 5
4 0 | 3 11 1 | 4 18 2 | 6 25 4 | 6
5 0 | 4 12 1 | 5 19 3 | 3 26 5 | 5
6 0 | 5 13 1 | 6 20 3 | 4 27 5 | 6
7 0 | 6 14 2 | 2 21 3 | 5 28 6 | 6
All the Double Six dominoes in a set can he laid out to display a 7 8 grid of pips. Each layout
corresponds at least one \map" of the dominoes. A map consists of an identical 7 8 grid with the
appropriate bone numbers substituted for the pip numbers appearing on that bone. An example of a
7 8 grid display of pips and a corresponding map of bone numbers is shown below.
7 x 8 grid of pips map of bone numbers
6 6 2 6 5 2 4 1 28 28 14 7 17 17 11 11
1 3 2 0 1 0 3 4 10 10 14 7 2 2 21 23
1 3 2 4 6 6 5 4 8 4 16 25 25 13 21 23
1 0 4 3 2 1 1 2 8 4 16 15 15 13 9 9
5 1 3 6 0 4 5 5 12 12 22 22 5 5 26 26
5 5 4 0 2 6 0 3 27 24 24 3 3 18 1 19
6 0 5 3 4 2 0 3 27 6 6 20 20 18 1 19
Write a program that will analyze the pattern of pips in any 78 layout of a standard set of dominoes
and produce a map showing the position of all dominoes in the set. If more than one arrangement of
dominoes yield the same pattern, your program should generate a map of each possible layout.
Input
The input le will contain several of problem sets. Each set consists of seven lines of eight integers
from 0 through 6, representing an observed pattern of pips. Each set is corresponds to a legitimate
conguration of bones (there will be at least one map possible for each problem set). There is no
intervening data separating the problem sets.
Output
Correct output consists of a problem set label (beginning with Set #1) followed by an echo printing of
the problem set itself. This is followed by a map label for the set and the map(s) which correspond to
the problem set. (Multiple maps can be output in any order.) After all maps for a problem set have
been printed, a summary line stating the number of possible maps appears.
At least three lines are skipped between the output from different problem sets while at least one
line separates the labels, echo printing, and maps within the same problem set.
Note: A sample input le of two problem sets along with the correct output are shown.
Sample Input
5 4 3 6 5 3 4 6
0 6 0 1 2 3 1 1
3 2 6 5 0 4 2 0
5 3 6 2 3 2 0 6
4 0 4 1 0 0 4 1
5 2 2 4 4 1 6 5
5 5 3 6 1 2 3 1
4 2 5 2 6 3 5 4
5 0 4 3 1 4 1 1
1 2 3 0 2 2 2 2
1 4 0 1 3 5 6 5
4 0 6 0 3 6 6 5
4 0 1 6 4 0 3 0
6 5 3 6 2 1 5 3
Sample Output
Layout #1:
5 4 3 6 5 3 4 6
0 6 0 1 2 3 1 1
3 2 6 5 0 4 2 0
5 3 6 2 3 2 0 6
4 0 4 1 0 0 4 1
5 2 2 4 4 1 6 5
5 5 3 6 1 2 3 1
Maps resulting from layout #1 are:
6 20 20 27 27 19 25 25
6 18 2 2 3 19 8 8
21 18 28 17 3 16 16 7
21 4 28 17 15 15 5 7
24 4 11 11 1 1 5 12
24 14 14 23 23 13 13 12
26 26 22 22 9 9 10 10
There are 1 solution(s) for layout #1.
Layout #2:
4 2 5 2 6 3 5 4
5 0 4 3 1 4 1 1
1 2 3 0 2 2 2 2
1 4 0 1 3 5 6 5
4 0 6 0 3 6 6 5
4 0 1 6 4 0 3 0
6 5 3 6 2 1 5 3
Maps resulting from layout #2 are:
16 16 24 18 18 20 12 11
6 6 24 10 10 20 12 11
8 15 15 3 3 17 14 14
8 5 5 2 19 17 28 26
23 1 13 2 19 7 28 26
23 1 13 25 25 7 4 4
27 27 22 22 9 9 21 21
16 16 24 18 18 20 12 11
6 6 24 10 10 20 12 11
8 15 15 3 3 17 14 14
8 5 5 2 19 17 28 26
23 1 13 2 19 7 28 26
23 1 13 25 25 7 21 4
27 27 22 22 9 9 21 4
There are 2 solution(s) for layout #2.

dfs的暴力题,注意输出格式控制,这里容易wa。
题目大意:给出一些7*8的矩阵,每两个相邻的数字可以表示一个骨牌,问说骨牌有多少种摆法。
解题思路:dfs枚举每一个位置,考虑当前位置和下面或右边组成的骨牌,直到所有位置都已安放好骨牌,则为一种方案。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define repd(i, a, b) for(int i = b; i >= a; i--)
#define sfi(n) scanf("%d", &n)
#define pfi(n) printf("%d\n", n)
#define sfi2(n, m) scanf("%d%d", &n, &m)
#define pfi2(n, m) printf("%d %d\n", n, m)
#define pfi3(a, b, c) printf("%d %d %d\n", a, b, c)
#define MAXN 105
#define R 6
#define C 7
const int INF = 0x3f3f3f3f;
const int dir[][] = {{, }, {, }};
int vis[][];
int mp[][];
int tot = ;
int d[][];
int hv[];
int kase = ;
int maxn; void get()
{
int t = ;
repu(i, , )
repu(j, i, ) d[j][i] = d[i][j] = t++;
} void put1()
{
printf("Layout #%d:\n\n", kase);
repu(i, , )
{
repu(j, , ) printf("%4d", mp[i][j]);
puts("");
}
puts("");
} void put2()
{
repu(i, , )
{
repu(j, , )
printf("%4d", vis[i][j]);
puts("");
}
puts("");
} bool Judge(int x, int y)
{
if(x >= && x <= R && y >= && y <= C) return true;
return false;
} void dfs(int x, int y)
{
if(x > R)
{
tot++;
put2();
}
else if(vis[x][y])
{
int dx = x;
int dy = y + ;
if(dy > C)
{
dx++;
dy = ;
}
dfs(dx, dy);
}
else
{
repu(i, , )
{
int dx = x + dir[i][];
int dy = y + dir[i][];
int t, t1, t2;
t1 = mp[x][y];
t2 = mp[dx][dy];
t = d[t1][t2];
if(Judge(dx, dy) && !hv[t] && !vis[dx][dy])
{
vis[dx][dy] = vis[x][y] = t;
hv[t] = ;
int tx = x, ty = y + ;
if(ty > C) tx++, ty = ;
dfs(tx, ty);
vis[dx][dy] = vis[x][y] = ;
hv[t] = ;
}
}
}
return ;
} int main()
{
get();
while(~sfi(mp[][]))
{
repu(i, , )
repu(j, , )
if(i || j) sfi(mp[i][j]);
_cle(vis, );
_cle(hv, );
tot = ;
maxn = ;
if(kase) printf("\n\n\n");
kase++;
put1();
printf("Maps resulting from layout #%d are:\n\n", kase);
dfs(, );
printf("There are %d solution(s) for layout #%d.\n", tot, kase);
}
return ;
}
uva 211(dfs)的更多相关文章
- Chinese Mahjong UVA - 11210 (DFS)
先记录下每一种麻将出现的次数,然后枚举每一种可能得到的麻将,对于这个新的麻将牌,去判断可不可能胡,如果可以胡,就可以把这张牌输出出来. 因为eye只能有一张,所以这个是最好枚举的,就枚举每张牌成为ey ...
- UVA 1640(DFS)
题意:给你a,b两个数 问你a b区间中0 9出现的次数 其实就是求1-n中0-9出现的次数 ans[n] 答案就是ans[b]-ans[a-1] 怎么求的话看代码吧 #include<io ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
随机推荐
- js接受url参数
1.正则表达式 function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=( ...
- Scala正则和抽取器:解析方法参数
在<正则表达式基础知识>中概括了正则表达式的基础知识, 本文讲解如何使用正则表达式解析方法参数,从而可以根据 DAO 自动生成 Service. 在做 Java 项目时,常常要根据 DAO ...
- 初识Python第二天(1)
在Python中,一切事物都是对象,对象是基于类创建的,对象继承了类的属性,方法等. 一.传递参数 1.1新建python文件,名为twoday_args.py,输出以下代码 import sys p ...
- POJ 2516:Minimum Cost(最小费用流)
https://vjudge.net/problem/11079/origin 题意:有N个商店和M个供应商和K种物品,每个商店每种物品有一个需求数,每个供应商每种物品有一个供应量,供应商到商店之间的 ...
- tcp粘包,udp丢包
TCP是面向流的, 流, 要说明就像河水一样, 只要有水, 就会一直流向低处, 不会间断. TCP为了提高传输效率, 发送数据的时候, 并不是直接发送数据到网路, 而是先暂存到系统缓冲, 超过时间或者 ...
- 【转】Struts1.x系列教程(2):简单的数据验证
转载地址:http://www.blogjava.net/nokiaguy/archive/2009/01/archive/2009/01/13/251197.html 简单验证从本质上说就是在服务端 ...
- Linq join on 多条件
var a = from m in DbContext.Set<T1>() join q in DbContext.Set<T2>() on new { m.ID, Phone ...
- Auty自动化测试框架第三篇——添加异常处理与日志收集
[本文出自天外归云的博客园] 本次对框架进行完善,增加了日志收集功能和修饰运行功能,完善后的lib目录如下:
- C中测试时间代码
- maven+Jenkins学习小记
jenkins配置方法1,tomcat下载,解压,切换到bin目录,配置环境变量,地址为catalina.bat文件夹下,也就是bin目录,再配置path变量2,启动tomcat,dos命令,cata ...