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