欧拉路的fleury算法 Domino

该博客介绍了如何利用Fleury算法解决多米诺骨牌排列挑战。给定一组每边标有0到6之间数字的多米诺骨牌,任务是将它们按照相同数字相邻的方式排列,允许旋转骨牌。输入包含骨牌的数量和每个骨牌的两个数字,输出是可能的排列方案或“无解”。示例输入和输出展示了具体的操作方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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 -

 

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std ;
const int Maxn = 210 ;

struct Node{
    int v, Next, sta, id ;
}no[Maxn];

int deg[10], head[10], cnt ;
int ans[Maxn][2], Vis[Maxn], tol ;
int n, m, x, y ;

void Add_edge(int u, int v, int id, int sta){
    no[cnt].v = v ;
    no[cnt].id = id ;
    no[cnt].sta = sta ;
    no[cnt].Next = head[u] ;
    head[u] = cnt++ ;
}

void solve (int x){
    for (int i = head[x]; i != -1; i = no[i].Next){
        int v = no[i].v , p = no[i].id, sta = no[i].sta ;
        if (!Vis[i]){
            Vis[i] = Vis[i ^ 1] = 1 ;
            solve(v) ;
            ans[++tol][0] = p ;
            ans[tol][1] = sta ;
        }
    }
}

int main(){
    cin >> n ;
    memset(head, -1, sizeof(head)) ;
    for (int i = 1; i <= n; i++){
        cin >> x >> y ;
        deg[x]++ ;
        deg[y]++ ;
        Add_edge(x, y, i, 1) ;
        Add_edge(y, x, i, 0) ;
    }
    int sum = 0 , s = -1 ;
    for (int i = 0; i <= 6; i++){
        if (deg[i] & 1) {
            sum++ ;
            s = i ;
        }
    }
    if (sum != 0 && sum != 2){
        cout << "No solution\n" ;
        return 0 ;
    }
    if (s != -1) solve(s) ;
    else {
        for (int i = 0; i <= 6; i++){
            if (deg[i] > 0){
                solve(i) ;
                break ;
            }
        }
    }
    if (tol != n){
        cout << "No solution\n" ;
        return 0 ;
    }
    for (int i = n; i >= 1; i--){
        printf ("%d %c\n", ans[i][0], ans[i][1] == 1 ? '+' : '-');
    }
    return 0 ;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值