sgu 101 Domino【输出欧拉路径】

101. Domino

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The blocks usually are called bones, dominoes, or pieces and sometimes men, stones, or even cards.
The face of each piece is divided, by a line or ridge, into two squares, each of which is marked as would be a pair of dice...

The principle in nearly all modern dominoes games is to match one end of a piece to another that is identically or reciprocally numbered.

ENCYCLOPÆDIA BRITANNICA

Given a set of domino pieces where each side is marked with two digits from 0 to 6. Your task is to arrange pieces in a line such way, that they touch through equal marked sides. It is possible to rotate pieces changing left and right side.

Input

The first line of the input contains a single integer N (1 ≤ N ≤ 100) representing the total number of pieces in the domino set. The following N lines describe pieces. Each piece is represented on a separate line in a form of two digits from 0 to 6 separated by a space.

Output

Write “No solution” if it is impossible to arrange them described way. If it is possible, write any of way. Pieces must be written in left-to-right order. Every of N lines must contains number of current domino piece and sign “+” or “-“ (first means that you not rotate that piece, and second if you rotate it).

Sample Input

5

1 2

2 4

2 4

6 4

2 1

Sample Output

2 -

5 +

1 +

3 +

4 -


题目大意:

有n个多米诺骨牌,每个骨牌上边有两个数字,我们想要排出能够通过一个骨牌能够推翻所有骨牌的方案,输出任意一个,相邻两个骨牌能够推倒的情况是对应连接的数字要相同,比如 1 2 2 4两个骨牌就能连在一起并且推倒、如果没有这样的方案,输出无解。


思路:


1、类似于词语接龙之类的题目,其实就是让你找一个欧拉路径或者是欧拉回路并且输出。


2、一共六个节点,不必要担心深搜超时,当然,深搜的姿势一定要对。


Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
struct node
{
    int from;
    int to;
    int next;
    int w;
    int num;
    int flag;
} e[2000+50];
int ans[150];
int head[10];
int degree[10];
int n,cont,ok,contz;
void add(int from,int to)
{
    e[cont].from=from;
    e[cont].to=to;
    e[cont].next=head[from];
    e[cont].num=cont;
    e[cont].flag=0;
    head[from]=cont++;
}
void Dfs(int x)
{
    for(int i=head[x];i!=-1;i=e[i].next)
    {
        if(e[i].flag==0)
        {
            e[i].flag=1;
            e[i^1].flag=1;
            Dfs(e[i].to);
            ans[contz++]=i;
        }
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int pos=0;
        cont=0;
        contz=0;
        memset(head,-1,sizeof(head));
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
            pos=x;
            degree[x]++;
            degree[y]++;
        }
        int tt=0;
        for(int i=0;i<=6;i++)
        {
            if(degree[i]%2==1)
            {
                tt++;
                pos=i;
            }
        }
        if(tt!=2&&tt!=0)
        {
            printf("No solution\n");
        }
        else
        {
            Dfs(pos);
            if(contz!=n)
            {
                printf("No solution\n");
            }
            else
            {
                for(int i=0;i<n;i++)
                {
                    printf("%d ",ans[i]/2+1);
                    if(ans[i]%2==0)
                    {
                        printf("-\n");
                    }
                    else printf("+\n");
                }
            }
        }
    }
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值