The Domino Effect (UVA - 211)

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 x 8 grid of pips. Each layout corresponds at least one ``map" of the dominoes. A map consists of an identical 7 x 8 grid with the appropriate bone numbers substituted for the pip numbers appearing on that bone. An example of a 7 x 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 7 x 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.

A sample input file 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.

题意:给一副图,代表多米诺骨牌摆放方式,每两个连成一块牌,如0 0 对应1号排 0 1 对应2号排,问图可以代表几种摆放方式。

本来没搞懂题意,迷糊了半天,理解清楚之后就很容易明白了,主要是控制DFS的方向(至少我是这样总结的这一题),代码主要控制DFS一行一行的的深搜。这里也是开拓了我的眼界。

上代码:

#include<cstdio>
#include<cstring>
using namespace std;
int T;
int used[30];
int book[9][9];
int tmp[9][9];
int out[9][9];
int nex_t[2][2]={ {1,0}, {0,1} };
int sum;
int dis[9][9]={
              {1,2,3,4,5,6,7},
              {2,8,9,10,11,12,13},
              {3,9,14,15,16,17,18},
              {4,10,15,19,20,21,22},
              {5,11,16,20,23,24,25},
              {6,12,17,21,24,26,27},
              {7,13,18,22,25,27,28}
              }; // 表明骨牌
bool init()
{
    if(scanf("%d", &tmp[0][0])==EOF) return false;
    for(int j=1;j<8;j++) scanf("%d" ,&tmp[0][j]);

    for(int i=1;i<7;i++)
        for(int j=0;j<8;j++)
            scanf("%d" ,&tmp[i][j]);
            if(T!=1) printf("\n\n\n");
    return true;
} //输入,注意EOF

void shuchu1()
{
    printf("Layout #%d:\n\n",T);
    for(int i=0;i<7;i++)
      for(int j=0;j<8;j++)
      {
         printf("%4d",tmp[i][j]);//注意输出格式%4d
         if(j==7)printf("\n");
      }
      printf("\n\n");
}

void shuchu2()
{
    for(int i=0;i<7;i++)
        for(int j=0;j<8;j++)
    {
        printf("%4d",out[i][j]);
        if(j==7) printf("\n");
    }
    printf("\n\n");
}

void dfs(int x,int y,int t)
{
    if(t==28) //DFS结束条件
    {
        sum++;
        if(sum==1)
            printf("Maps resulting from layout #%d are:\n\n",T);
        shuchu2();
        return ;
    }

    if(x>=7) return ; 
    if(y>=8)
    {
        for(int i=0;i<8;i++) if(book[x][i]==0) return;
        //如果这一行的每一个数都使用过了,就深搜下一行;否则结束。
        dfs(x+1,0,t);
        return;
    }
    if(book[x][y]==1) {dfs(x,y+1,t);return;} // 这种情况考虑到nex_t[1];仔细想一下!
    int xx,yy;
    for(int i=0;i<2;i++)
    {
      if(x==6&&i==0) continue;
      if(y==7&&i==1) continue;
      xx = x + nex_t[i][0];
      yy = y + nex_t[i][1];
      int p1 = tmp[x][y];
      int p2 = tmp[xx][yy];
      int val = dis[p1][p2];
      if(book[x][y]==1||book[xx][yy]==1||used[val]==1)continue;
      out[x][y] = out[xx][yy] = val;
      book[x][y] = book[xx][yy] = used[val] = 1;
      dfs(x,y+1,t+1);
      book[x][y] = book[xx][yy] = used[val] = 0; //以上就是常规的DFS,没啥特点了。
    }
    return ;
}
int main()
{
    T = 1;
    while(init())
    {
        memset(book,0,sizeof(book));
        memset(used,0,sizeof(used));
        sum = 0;
        shuchu1();
        dfs(0,0,0);
        printf("There are %d solution(s) for layout #%d.\n", sum,T);
        T++;
    }
    return 0;
}
// 这题告诉我们要懂得变通,哈哈哈哈哈哈嗝。



水波。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值