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->4-6这样的顺序,每个牌能反转顺序:1-2可以变成2-1,最后输出牌推到的顺序时,按原始顺序推到的牌输出“牌号 +”,反转了的则输出“牌号 -”。

解题思路:读懂题目后其实就是一个欧拉通路,把每个牌当作一条边,推到所有的牌就是找到一个欧拉路径即可,其实给的是有向图,因为我们可以反转,所以我们还是选择构造无向图,经过一条边时同时割掉两条边即可,输出时,因为正确的边一定是偶数,所以是奇数边我们输出“-”,特别注意:因为我们dfs最终得到的是反向的路径,所以输出时的+-还是要看你的遍历方式!还要注意是否为连通图!
AC代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, a, b) G[a].push_back(b);
using namespace std;
typedef long long LL;
typedef pair par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

int n, cnt, sum;
int head[10], vis[110*2], du[10], path[110];

struct edge
{
    int to;
    int next;
    int id;
}e[110*2];

void add(int u, int v)
{
    e[cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt ++;
}

void dfs(int k)
{
    for(int i = head[k]; i != -1; i = e[i].next) {
        int en = e[i].to;
        if(!vis[i]) {
            vis[i] = 1;
            vis[i^1] = 1; //割掉另一条边
            dfs(en);
            path[sum ++] = i; //一定要在dfs以后再记录边啊!
        }
    }
}

int main()
{
    int x, y;
    while(~scanf("%d", &n)) {
        mem1(head);
        mem0(vis);
        mem0(du);
        cnt = 0;
        sum = 0;
        int flag = 0, st;
        for(int i = 1; i <= n; i ++) {
            in2(x, y);
            add(x, y);
            add(y, x);
            du[x] ++;
            du[y] ++;
            st = x; //这里要特别注意初始化起点
        }
        for(int i = 0; i <=6 ; i ++) {
            if(du[i]&1) {
                flag ++;
                st = i; //有奇度点则奇度点作为为起点
            }
        }
        if(flag == 0 || flag == 2) {
            dfs(st);
            if(sum != n) printf("No solution\n"); //判断连通图
            else
            for(int i = 0; i < sum; i ++) { //正向为 sum-1~0遍历
                printf("%d ", (path[i]+2)/2); //输出牌号
                if(path[i]&1) { //因为我是反向的路径
                    printf("+\n");
                }else printf("-\n");
            }
        }else {
            printf("No solution\n");
        }
    }
    return 0;
}


你可能感兴趣的:(ACM_图论,ACM_欧拉回路)