SGU 101

6 篇文章 0 订阅

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 -


蛮无语的,其实很简单,建7个点,然后骨牌当边就行,求一条欧拉通路。


#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>

#define Clear(a,b) memset(a,b,sizeof(a))

using namespace std;

struct edge
{
	int ed,id;
}tmp;

const bool isOJ = 0 ,Debug = 0;
//
const int maxn = 200,maxp = 200;
const char NoS[100] = "No solution";

vector<edge> p[maxp];
int n;
int ans_id[maxn],ans_dir[maxn],a[maxn],b[maxn],ma[maxn][maxn],deg[maxp],tmp_id[maxn],tmp_dir[maxn];
bool vis[maxn],vis2[maxn],FindAns;

void Dfs(int now,int k){
	if (FindAns)
		return ;
	if (Debug) printf("xx %d %d\n",now,k);
	int i,j;
	if (k > n){
		FindAns = 1;
		for (i = 1;i <= n;i++){
			ans_id[i] = tmp_id[i];
			ans_dir[i] = tmp_dir[i];
			//printf("fuckxx %d %d\n",tmp_id[i],tmp_dir[i]);
		}
		if (Debug) printf("fuck\n");
		return ;
	}
	int m = p[now].size();
	for (j = 0;j < m;j++){
		int id = p[now][j].id,ed = p[now][j].ed;
		if (vis2[id] == 0){
			vis2[id] = 1;
			tmp_id[k] = id;
			tmp_dir[k] = ed == b[id];
			//printf("xxoo %d %d",id,ed);
			Dfs(ed,k+1);
			vis2[id] = 0;
		}
	}
}

int Work(){
	int i,j,k,now,NO,m,num_odd;
	if (scanf("%d",&n) == EOF) return 0;
	for (i = 0;i <= 6;i++)
		p[i].clear();
	Clear(ma,0);
	Clear(a,0);
	Clear(b,0);
	Clear(vis,0);
	Clear(vis2,0);
	Clear(deg,0);
	for (i = 1;i <= n;i++){
		scanf("%d%d",&a[i],&b[i]);
		tmp.id = i;
		tmp.ed = a[i];
		p[b[i]].push_back(tmp);
		tmp.ed = b[i];
		p[a[i]].push_back(tmp);
		ma[a[i]][b[i]] = 1;
		ma[b[i]][a[i]] = 1;
		vis[a[i]] = 1;
		vis[b[i]] = 1;
	}
	//判断是否联通
	NO = 0;
	for (i = 0;i <= 6;i++)
		for (j = 0;j <= 6;j++)
			for (k = 0;k <= 6;k++)
				ma[i][j] = ma[i][j] || (ma[i][k] && ma[k][j]);
	for (i = 0;i <= 6;i++)
		for (j = 0;j <= 6;j++)
			NO = NO || (vis[i] && vis[j] && (ma[i][j] == 0));
	//统计奇点个数
	num_odd = 0;
	for (i = 0;i <= 6;i++)
		num_odd += (p[i].size()&1);
	//如果无解,输出并退出
	if (Debug){
		printf("%d %d\n",NO,num_odd);
		for (i = 0;i <= 6;i++)
			printf("%d %d\n",i,(int)p[i].size());
	}
	if (NO || ((num_odd != 0) && (num_odd != 2))){
		printf("%s\n",NoS);
		return 1;
	}
	//有解,搜索可行解
	//-先查找起始点
	if (num_odd == 2)
		for (i = 0;i <= 6 && ((p[i].size()&1)==0);i++);
	else
		for (i = 0;i <= 6 && vis[i] == 0;i++);
	now = i;
	for (i = 0;i <= 6;i++)
		deg[i] = p[i].size();
	//-找到之后开始干活
	FindAns = 0;
	Dfs(now,1);
	
	//验证
	if (0){
	for (i = 2;i <= n;i++){
		int t1,t2;
		int id = ans_id[i],id2 = ans_id[i-1];
		if (ans_dir[i-1] == 1)
			t1 = b[id2];
		else
			t1 = a[id2];
		if (ans_dir[i] == 1)
			t2 = a[id];
		else
			t2 = b[id];
		if (t1 != t2){
			//printf("------------%d %d %d %d %d\n",i,id2,id,t1,t2);
			while(1);
		}
	}
	}

	//
	for (i = 1;i <= n;i++)
		printf("%d %c\n",ans_id[i],ans_dir[i]==1?'+':'-');
	return 1;
}

int main(){
	if (!isOJ){
		freopen("/Users/mac/Desktop/test.in","r",stdin);
	}
	while(Work());
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值