SGU101

SGU link is here

101 Domino

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.

ENCYCLOPAEDIA 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 -

Solution

We only need to find the Euler path. Every domino is an edge. The vertices are 1~7.

Fleury’s Algorithm (Euler trial)

Let $G$ be a connected graph. If $G$ is Eulerian then Fleury's Algorithm will produce an Eulerian trial in $G$.

In a connected graph $G$, a bridge is an edge which, if removed, produces a disconnected graph.

Fleury's Algorithm

  1. Choose any vertex $v$ of $G$ and set current vertex equal to $v$ and current trail equal to the empty sequence of edges

  2. Select any edge $e$ incident with the current vertex but choosing a bridge only if there is no alternative

  3. Add $e$ to the current trail and set the current vertex equal to the vertex at the other end of $e$. if $e$ is a loop, the current vertex will not move

  4. Delete $e$ from the graph. Delete any isolated vertices. Repeate steps 2 - 4 untill all edges have been deleted from $G$. The final current trail is an Eulerian trail in $G$

Code

#include <iostream>
using namespace std;
struct D
{
    int right,left;
};
D d[100];
int n;
int ans[100];
char op[100];
int degree[7];
int g[7][7];
bool finder;
bool use[100];
int find(int l,int r,int k)
{
    int k1,k2;
    for(int i = 0;i<n;i++)
    {
        if(use[i])
            continue;
        k1 = d[i].right;
        k2 = d[i].left;
        if(k1 == r&&k2 == l)
        {
            ans[k] = i + 1;
            op[k] = '+';
            k++;
            use[i] = true;
            return i;
        }
        else
            if(k1 == l&&k2 == r)
            {
                ans[k] = i + 1;
                op[k] = '-';
                k++;
                use[i] = true;
                return i;
            }
    }
}
void DFS(int v,int kb,int g1[][7])
{    
    if(finder)
        return;
    if(kb == n)
    {
        finder = true;
        for(int i = 0;i<n;i++)
            cout<<ans[i]<<" "<<op[i]<<endl;;
        return;
    }
    for(int i = 0;i<7;i++)
    {
        if(g1[v][i] > 0)
        {
            int kl1 = find(v,i,kb);
            g1[i][v]--;
            g1[v][i]--;
            DFS(i,kb + 1,g1);
            use[kl1] = false;
            g1[i][v]++;
            g1[v][i]++;
        }
    }
}
int main()
{
    cin>>n;
    int start;
    for(int i = 0;i<n;i++)
    {
        cin>>d[i].left>>d[i].right;
        start = d[i].right;
        g[d[i].left][d[i].right]++;
        g[d[i].right][d[i].left]++;
        degree[d[i].left]++;
        degree[d[i].right]++;
    }
    int oddp = 0;
    for(int i = 0;i<=6;i++)
    {
        if(degree[i]%2)
        {
            start = i;
            oddp++;
        }
    }
    if(oddp > 2)
    {        
        cout<<"No solution"<<endl;
        return 0;
    }
    DFS(start,0,g);
    if(!finder)
    {        
        cout<<"No solution"<<endl;
        return 0;
    }
}

}