SGU101 - Domino

101. Domino

time limit per test: 0.5 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个多米诺骨牌,多米诺骨牌的两面都有一个点数,范围是[0,6]。任务是把骨牌排成一条直线,使得相邻之间的数字相等。
题解:就是欧拉路径问题。WA了好多次,o(╯□╰)o。第一次是由于没有考虑欧拉回路,第二次WA是没有判断所有点是否连通,直接边搜索边输出。。。。话说SGU的测试数据组数好多。。。不过这样也非常好,促使你必须充分考虑各种情况。
View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 typedef struct
 4 {
 5     int x;
 6     int y;
 7 } NODE;
 8 int len;
 9 int graph[10][10],deg[10];
10 NODE f[105],vv[105];
11 void Euler(int start)
12 {
13     int i;
14     for(i=0; i<=6; i++)
15         if(graph[start][i])
16         {
17             graph[start][i]--;
18             graph[i][start]--;
19             Euler(i);
20             len++;
21             vv[len].x=start;
22             vv[len].y=i;
23         }
24 }
25 int main(void)
26 {
27     int n,i,x,y,j;
28     scanf("%d",&n);
29     for(i=1; i<=n; i++)
30     {
31         scanf("%d%d",&x,&y);
32         deg[x]++;
33         deg[y]++;
34         graph[x][y]++;
35         graph[y][x]++;
36         f[i].x=x;
37         f[i].y=y;
38     }
39     for(i=0; i<=6; i++)
40     {
41         if(deg[i])
42         {
43             x=i;
44             break;
45         }
46     }
47     j=0;
48     for(i=0; i<=6; i++)
49         if(deg[i]%2==1)
50         {
51             j++;
52             x=i;
53         }
54     len=0;
55     if(j!=0&&j!=2)
56     {
57         printf("No solution\n");
58         return 0;
59     }
60     Euler(x);
61     if(len<n)
62     {
63         printf("No solution\n");
64         return 0;
65 
66     }
67     else
68     {
69           for(i=len;i>=1;i--)
70           for(j=1;j<=n;j++)
71           if(vv[i].x==f[j].x&&vv[i].y==f[j].y)
72           {
73               printf("%d +\n",j);
74               f[j].x=-1;
75               break;
76           }
77           else
78           if(vv[i].x==f[j].y&&vv[i].y==f[j].x)
79           {
80               printf("%d -\n",j);
81               f[j].x=-1;
82               break;
83 
84           }
85     }
86     return 0;
87 }

 



posted on 2013-03-08 03:06  仗剑奔走天涯  阅读(291)  评论(0编辑  收藏  举报

导航