【SGU 101】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 - 

  开刷SGU!
  题目就是让你找一个欧拉路,没什么好说的,dfs搞定。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#define mm 11000
using namespace std;

int n,a,b,degree[mm];
bool vis[mm]={false};

struct EDGE{
	int pnt,idx;
	EDGE *pre;
	EDGE(){}
	EDGE(int _pnt,int _idx,EDGE *_pre):pnt(_pnt),idx(_idx),pre(_pre){}
}Edge[mm*2],*SP=Edge,*edge[mm];

inline void addedge(int a,int b,int idx){
	edge[a]=new(++SP)EDGE(b,idx,edge[a]);
	edge[b]=new(++SP)EDGE(a,-idx,edge[b]);
	degree[a]++,degree[b]++;
}

void dfs(int x){
	for(EDGE *j=edge[x];j;j=j->pre)
		if(!vis[abs(j->idx)]){
			vis[abs(j->idx)]=true;
			dfs(j->pnt);
		}
}

void Output(int x){
	for(EDGE *j=edge[x];j;j=j->pre)
		if(!vis[abs(j->idx)]){
			vis[abs(j->idx)]=true;
			Output(j->pnt);
			printf("%d %c\n",abs(j->idx),j->idx>0?'-':'+');
		}
}

void Halt(){
	printf("No solution\n");
	exit(0);
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a,&b);
		addedge(a,b,i);
	}
	int source=0,cnt=0;
	for(int i=0;i<7;i++)
		if(degree[i]&1) source=i,cnt++;
	if(cnt && cnt!=2) Halt();
	for(int i=0;i<7,source==0;i++)
		if(degree[i]){
			source=i;
			break;
		}
	dfs(source);
	for(int i=1;i<=n;i++)
		if(!vis[i]) Halt();
	memset(vis,false,sizeof(vis));
	Output(source);
	return 0;
}

posted @ 2011-08-16 12:17  Delostik  阅读(368)  评论(0编辑  收藏  举报