您的位置:首页 > 其它

SGU101 - Domino

2013-03-08 03:06 309 查看
[b]101. Domino[/b]time limit per test: 0.5 sec.
memory limit per test: 4096 KBDominoes – 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 BRITANNICAGiven 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.InputThe 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.OutputWrite “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
#include<stdio.h>
#include<string.h>
typedef struct
{
int x;
int y;
} NODE;
int len;
int graph[10][10],deg[10];
NODE f[105],vv[105];
void Euler(int start)
{
int i;
for(i=0; i<=6; i++)
if(graph[start][i])
{
graph[start][i]--;
graph[i][start]--;
Euler(i);
len++;
vv[len].x=start;
vv[len].y=i;
}
}
int main(void)
{
int n,i,x,y,j;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
scanf("%d%d",&x,&y);
deg[x]++;
deg[y]++;
graph[x][y]++;
graph[y][x]++;
f[i].x=x;
f[i].y=y;
}
for(i=0; i<=6; i++)
{
if(deg[i])
{
x=i;
break;
}
}
j=0;
for(i=0; i<=6; i++)
if(deg[i]%2==1)
{
j++;
x=i;
}
len=0;
if(j!=0&&j!=2)
{
printf("No solution\n");
return 0;
}
Euler(x);
if(len<n)
{
printf("No solution\n");
return 0;

}
else
{
for(i=len;i>=1;i--)
for(j=1;j<=n;j++)
if(vv[i].x==f[j].x&&vv[i].y==f[j].y)
{
printf("%d +\n",j);
f[j].x=-1;
break;
}
else
if(vv[i].x==f[j].y&&vv[i].y==f[j].x)
{
printf("%d -\n",j);
f[j].x=-1;
break;

}
}
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: