UVA 211 (DFS)

Source: Internet
Author: User

211-the Domino Effect

Time limit:3.000 seconds

A standard set of Double Six dominoes contains pieces (called bones) displaying
from 0 (blank) to 6 using Dice-like pips. The bones, which is 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 would analyze the pattern of pips in any, 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 maps of each possible layout.
Input
The input le'll 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 are corresponds to a legitimate
Conguration of Bones (there'll be in 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 any maps for a problem set has
been printed, a summary line stating the number of possible maps appears.
At least three lines is 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 problem sets along with the correct output is 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 is:
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 is 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 is:
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 is 2 solution (s) for layout #2.

DFS violence problem, note output format control, here Easy WA.

The main topic: give some 7*8 matrix, each two adjacent numbers can represent a domino, ask how many kinds of dominoes pendulum method.

How to solve the problem: Dfs enumerates every location, considering the current position and the domino of the bottom or right, until all positions have been placed in a domino, is a scenario.

#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 namespacestd;#definell Long Long#define_cle (M, a) memset (M, A, sizeof (m))#defineRepu (I, A, b) for (int i = A; I < b; i++)#defineREPD (I, A, b) for (int i = b; i >= A; i--)#defineSFI (n) scanf ("%d", &n)#definePFI (n) printf ("%d\n", N)#defineSfi2 (n, m) scanf ("%d%d", &n, &m)#definePfi2 (n, m) printf ("%d%d\n", N, m)#definePfi3 (A, B, c) printf ("%d%d%d\n", A, B, c)#defineMAXN 105#defineR 6#defineC 7Const intINF =0x3f3f3f3f;Const intdir[][2] = {{1,0}, {0,1}};intvis[7][8];intmp[7][8];inttot =0;intd[7][7];inthv[ in];intKase =0;intMAXN;void Get(){    intt =1; Repu (i,0,7) Repu (J, I,7) D[j][i] = d[i][j] = t++;}voidput1 () {printf ("Layout #%d:\n\n", Kase); Repu (i,0,7) {Repu (J,0,8) printf ("%4d", Mp[i][j]); Puts (""); } puts ("");}voidPut2 () {Repu (I,0,7) {Repu (J,0,8) printf ("%4d", Vis[i][j]); Puts (""); } puts ("");}BOOLJudge (intXinty) {    if(x >=0&& x <= R && y >=0&& y <= C)return true; return false;}voidDfsintXinty) {    if(X >R) {tot++;    Put2 (); }    Else if(Vis[x][y]) {intDX =x; intDy = y +1; if(Dy >C) {DX++; Dy=0;    } dfs (dx, dy); }    Else{Repu (i,0,2)        {            intDX = x + dir[i][0]; intDy = y + dir[i][1]; intT, 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; inttx = x, Ty = y +1; if(Ty > C) tx++, Ty =0;                DFS (TX, Ty); Vis[dx][dy]= Vis[x][y] =0; Hv[t]=0; }        }    }    return ;}intMain () {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 is%d solution (s) for layout #%d.\n", Tot, Kase); }    return 0;}
View Code

UVA 211 (DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.