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 di?erent 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[][2] = {{1, 0}, {0, 1}};
int vis[7][8];
int mp[7][8];
int tot = 0;
int d[7][7];
int hv[29];
int kase = 0;
int maxn;

void get()
{
    int t = 1;
    repu(i, 0, 7)
    repu(j, i, 7)  d[j][i] = d[i][j] = t++;
}

void put1()
{
    printf("Layout #%d:\n\n", kase);
    repu(i, 0, 7)
    {
        repu(j, 0, 8) printf("%4d", mp[i][j]);
        puts("");
    }
    puts("");
}

void put2()
{
    repu(i, 0, 7)
    {
        repu(j, 0, 8)
        printf("%4d", vis[i][j]);
        puts("");
    }
    puts("");
}

bool Judge(int x, int y)
{
    if(x >= 0 && x <= R && y >= 0 && 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 + 1;
        if(dy > C)
        {
            dx++;
            dy = 0;
        }
        dfs(dx, dy);
    }
    else
    {
        repu(i, 0, 2)
        {
            int dx = x + dir[i][0];
            int dy = y + dir[i][1];
            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] = 1;
                int tx = x, ty = y + 1;
                if(ty > C) tx++, ty = 0;
                dfs(tx, ty);
                vis[dx][dy] = vis[x][y] = 0;
                hv[t] = 0;
            }
        }
    }
    return ;
}

int main()
{
    get();
    while(~sfi(mp[0][0]))
    {
        repu(i, 0, 7)
        repu(j, 0, 8)
        if(i || j) sfi(mp[i][j]);
        _cle(vis, 0);
        _cle(hv, 0);
        tot = 0;
        maxn = 0;
        if(kase) printf("\n\n\n");
        kase++;
        put1();
        printf("Maps resulting from layout #%d are:\n\n", kase);
        dfs(0, 0);
        printf("There are %d solution(s) for layout #%d.\n", tot, kase);
    }
    return 0;
}

时间: 2024-10-25 14:47:15

uva 211(dfs)的相关文章

UVA Transportation (DFS)

 Transportation  Ruratania is just entering capitalism and is establishing new enterprising activities in many fields including transport. The transportation company TransRuratania is starting a new express train from city A to city B with several st

uva :185 - Roman Numerals(dfs)

题目:uva :185 - Roman Numerals 题目大意:给出一个字符串的等式,问这个字符串是否是罗马等式吗?有符合的阿拉伯等式吗?前者是就输出correct or incorrect ,后者就得分情况: ambiguous 能组成阿拉伯等式的字母组合大于等于2, valid 能组成阿拉伯等式的字母组合只有1种 impossible 没有符合阿拉伯等式的字母组合. 解题思路: 1.能不能组成罗马等式的规则:每个当前的字母(i)的左边的字母((i-1)所对应的值如果比i所对应的值小的话,

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

LeetCode Subsets (DFS)

题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 return a

POJ 1699 Best Sequence(DFS)

題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做的 1 //1699 2 #include <iostream> 3 #include <stdio.h> 4 #include <string.h> 5 #include <string> 6 7 using namespace std; 8 9 string

LeetCode Subsets II (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

poj A Knight&#39;s Journey(DFS)

题目链接:http://acm.hrbust.edu.cn/vj/index.php?c=problem-problem&id=190592 题意:给出p*q的棋盘,从(A,1)开始,走“日”字,问能否走完棋盘上所有的点,如果能,按字典序输出路径: 思路:DFS,并保存路径即可,注意处理走的方向顺序int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}}; #include <stdio.h> #in

11218 - KTV(dfs)

问题 C: Repeat Number 时间限制: 1 Sec  内存限制: 128 MB 提交: 23  解决: 7 [提交][状态][论坛] 题目描述 Definition: a+b = c, if all the digits of c are same ( c is more than ten),then we call a and b are Repeat Number. My question is How many Repeat Numbers in [x,y]. 输入 There

POJ 2488-A Knight&#39;s Journey(DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10813 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar