C . The Domino Effect

Description

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:

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.

Write a program that will analyze the pattern of pips in any 7×8 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 file 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 configuration 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 file of two problem sets along with the correct output are shown.

Samples

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

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.

有一副多米诺骨牌,包含28张,每个多米诺骨牌正反面各有一个数字。在7 * 8的网格中各张牌各摆一张,左边是个格子的典树,右边是格子所属的骨牌序号,要求输出所有可能的骨牌序号。

搜索与回溯,注意往右和下搜索,当搜索到最右侧的时候,跳回最左侧。这样才可以遍历所有的节点。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 10;
int pic[maxn][maxn];
bool use[30];
int code[maxn][maxn];
int dx[] = {0, 1}; // 上 左 下 右
int dy[] = {1, 0};
int cnt = 0;

void printO() {
    for(int i = 0; i < 7; i++) {
        for(int j = 0; j < 8; j++) {
            cout << setw(4) << pic[i][j];
        }      
        cout << endl; 
    }
    cout << endl;
}

void read() {
    for(int i = 1; i < 8; i++) {
        cin >> pic[0][i];
    }
    for(int i = 1; i < 7; i++) {
        for(int j = 0; j < 8; j++) {
            cin >> pic[i][j];
        }
    }
    printO();
}

void print() {
    cout << endl;
    for(int i = 0; i < 7; i++) {
        for(int j = 0; j < 8; j++) {
            cout << setw(4) << code[i][j];
        }
        cout << endl;
    }
}

bool check() {
    for(int i = 0; i < 7; i++) {
        for(int j = 0; j < 8; j++) {
            if(code[i][j] == -1) return false;
        }
    }
    return true;
}

int getCode(int x, int y) {
    return 7 * x + y - (x + x * x) / 2 + 1;
}

void dfs(int d, int maxd, int x, int y) {
    if(d == maxd) {
        if(check()) {
            print();
            cnt++;
            cout << endl;
        }
        return;
    }
    if(y == 8) {
        x++; 
        y = 0;
    }
    if(code[x][y] != -1) {
        dfs(d, maxd, x, y + 1);
        return;
    }
    int xx, yy;
    for(int i = 0; i < 2; i++) {
        xx = x + dx[i];
        yy = y + dy[i];
        if(xx < 0 || xx >= 7 || yy < 0 || yy >= 8) continue;
        if(code[xx][yy] != -1) continue;
        int c1 = pic[x][y];
        int c2 = pic[xx][yy];
        if(c1 > c2) swap(c1, c2);
        int c = getCode(c1, c2);
        if(!use[c]) {
            code[x][y] = code[xx][yy] = c;
            use[c] = true;
            dfs(d + 1, maxd, x, y + 1);
            use[c] = false;
            code[x][y] = code[xx][yy] = -1;
        }
    } 
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int kase = 0;
    while(cin >> pic[0][0]) {
        cnt = 0;
        if(kase)  cout << "\n\n\n\n\n";
        cout << "Layout #" << ++kase << ":\n\n\n";
        read();
        memset(code, -1, sizeof(code));   
        memset(use, 0, sizeof(use)); 
        cout << "Maps resulting from layout #" << kase << " are:\n\n";
        dfs(0, 28, 0, 0);
        cout << "\nThere are "  << cnt << " solution(s) for layout #"  << kase << ".\n";
    }
    return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值