SGU[101] Domino

Source: Internet
Author: User

DescriptionDescribe

Dominoes–game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or P IPs, on it face. The blocks usually is called bones, dominoes, or pieces and sometimes men, stones, or even cards.

The face of each piece are divided, by a line or ridge, into and squares, each of the which are marked as would be a pair of dic E...

The principle in nearly all modern dominoes games are to match one end of a piece to another so is identically or recipro Cally numbered.

Encyclopædia BRITANNICA

Dom-use small, rectangular blocks of wood or other materials, each with a bit of surface marking. Such blocks are often called dominoes, dominoes, or strips. Sometimes people, dominoes and even cards are used.

Each block is a straight line or divided into two sides, each side is marked on a pair of dice ...

In the modern Domino game, the rule is to make two labels identical or corresponding to the two dominoes end-to-end connection.

-Encyclopedia of the Britannica

Given a set of domino pieces where each side are marked with the digits from 0 to 6. Your task is to arrange pieces in a line such the, that they touch through equal marked sides. It's possible to rotate pieces changing left and right side.

Given a series of dominoes, each side is labeled with two numbers from 0 to 6. Your task is to arrange such sequences so that the dominoes are connected to the left and right, and the numbers of the connected polygons are the same. Dominoes can be flipped to make the left and right digital exchanges.

InputInput

The first line of the input contains a single integer N (1≤n≤100) representing the total number of pieces in the Domin o set. The following N lines describe pieces. Each piece was represented on a separate line in a form of the digits from 0 to 6 separated by a space.

The first line of input contains an integer n (1 <= N <= 100) that represents the total number of dominoes. The next n lines describe the state of the domino. Each row contains two numbers from 0 to 6.


OutputOutput

Write "No Solution" if it is the impossible to arrange them described. If It is a possible, write any of the. Pieces must is written in left-to-right order. Every of N lines must contains number of current Domino piece and sign "+" or "-" (first means so you don't rotate that pi ECE, and second if you rotate it).

If no such arrangement exists, output "no solution". If such an arrangement exists, output any one of the scenarios. You must output from left to right, output a total of n rows, each line contains a number of the current domino, and a mark "+" or "-" ("+" means not to reverse the domino, "-" represents the reversal of the domino).


Sample InputSample input

5
1 2
2 4
2 4
6 4
2 1

Sample OutputSample output

2-

1 +
3 +
4-

AnalysisAnalysis

This problem has been done for a long time, finally AC.

The first time I saw this topic was completely unaware of how to do, the second time feel can dfs. Later read the puzzle, found that is Oraton road.

We look at 0-6 of these 7 numbers as points, each domino as a non-edge, to build a picture.

This side uses a trick, when we set the pvisited array, we do not have to determine whether the point is accessed, we can also be used to determine whether the edge is accessed, so that we can use the edge set array to save the picture.

Euler's path is a stroke problem, which distinguishes it from the Euler circuit (which requires a return to the starting point).

The idea is simple, first determine the starting point:

    • The total number of points with odd degrees is 0, and any point is selected as the starting point;
    • The total number of points with odd degrees is 2, and a point in any of the two points is the starting point;
    • In other cases there is no solution.

The next is to write the Euler pathway, before a noip on a reference to see a DFS version, press it to write again, always WA, and later found that his method seems to have a problem.

In addition, it is also necessary to judge the self-ring, that is, if the graph is not connected, then obviously there is no solution.

Also, when you choose the starting point, you can return to a point, you need to note that this point must be the point of occurrence, otherwise it will WA.

After so much thinking, re-write not know how many times the code, finally AC.

Finally, the case of "s" in "No solution" is PE once.

SolutionSolution Solutions
#include <iostream> #include <memory.h>using namespace std;const int MAX = 128;const int SIZE = 8;struct node{i NT S, E;}; struct Path{int X;char y;}; int N;bool bflag;bool pvisited[max];int pdegree[size], pmap[size][size]; Node Pnode[max]; Path ppath[max];int Check (), void Dfs (int nstart, int nflag, int nstep), int main () {int x, y;while (cin >> N) {bflag = f  Alse;memset (pMap, 0, sizeof (PMAP)), memset (pvisited, False, sizeof (pvisited)); for (int i = 1; I <= N; i++) {cin >> X >> y;pmap[x][y]++; pmap[y][x]++;p node[i].s = x; pnode[i].e = y;} int nstart = Check (), if (Nstart = =-1) {cout << "No solution" << Endl;} else{for (int i = 1; I <= N; i++) {if (Pnode[i].s = = Nstart) {DFS (I, 1, 1); break;} else if (pnode[i].e = = Nstart) {DFS (I, 2, 1); break;}}} if (!bflag) {cout << "No solution" << Endl;}}} int Check () {memset (pdegree, 0, sizeof (pdegree)), int ncnt = 0, NPos = pnode[1].s, for (int i = 0; i < SIZE; i++) {for (int j = 0; J < SIZE; J + +) {PdegreE[i] + = pmap[i][j]; }}for (int i = 0; i < SIZE; i++) {if (Pdegree[i]% 2) {ncnt++; nPos = i;}} if (ncnt = = 0 | | ncnt = = 2) {return nPos;} else {return-1;}} void Dfs (int nstart, int nflag, int nstep) {if (bflag) {return;} Pvisited[nstart] = true;ppath[nstep].x = Nstart;ppath[nstep].y = (Nflag = = 1)? ' + ': '-'; if (nstep = = N) {for (int i = 1; I <= N; i++) {cout << ppath[i].x << "" << ppath[i].y <&lt ; Endl }bflag = true;} int NNext = (Nflag = = 1)? pnode[nstart].e:pnode[nstart].s;for (int i = 1; I <= N; i++) {if (!pvisited[i]) {if (Pnode[i].s = nnext) {DFS (I, 1, nste p + 1); }else if (pnode[i].e = = NNext) {DFS (I, 2, nstep + 1);} Pvisited[i] = false;}}}

  

Finally AC this problem, wrote for a long time.

SGU[101] Domino

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.