UVA - 10129 Play on Words (欧拉路径+dfs)

Play on Words

Some of the secret doors contain a very interesting word puzzle. The team of archaeologists has to solve it to open that doors. Because there is no other way to open the doors, the puzzle is very important for us.

There is a large number of magnetic plates on every door. Every plate has one word written on it. The plates must be arranged into a sequence in such a way that every word begins with the same letter as the previous word ends. For example, the word ``acm'' can be followed by the word ``motorola''. Your task is to write a computer program that will read the list of words and determine whether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open the door.

Input Specification

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer number Nthat indicates the number of plates (1 <= N <= 100000). Then exactly Nlines follow, each containing a single word. Each word contains at least two and at most 1000 lowercase characters, that means only letters 'a' through 'z' will appear in the word. The same word may appear several times in the list.

Output Specification

Your program has to determine whether it is possible to arrange all the plates in a sequence such that the first letter of each word is equal to the last letter of the previous word. All the plates from the list must be used, each exactly once. The words mentioned several times must be used that number of times.

If there exists such an ordering of plates, your program should print the sentence "Ordering is possible.". Otherwise, output the sentence "The door cannot be opened.".

Sample Input

32acmibm3acmmalformmouse2okok

Output for the Sample Input

The door cannot be opened.Ordering is possible.The door cannot be opened.


题目翻译:

有一些秘密的门包含着非常有趣的单词迷题, 考古学家队伍必须解决它们才能够打开大门。 因为没有其他方法能偶打开这些门, 所以解决那些迷题对我们非常重要。

在每个门上有很多个有磁力的盘子,盘子上面写着单词。 必须重新移动放置这些盘子,让它们形成一个队列:队列中,除了第一个单词,每个单词的开头和上一个单词的结尾字母一样。例如, motorola的后面可以接上acm。

你的任务是写一个程序, 读入一系列单词,然后计算确定它们是否有可能被排成这样的队列。

解析:
可以将这些单词看成是图的边,问你是否存在一条欧拉路径使所有的边相连。

欧拉道路概念:
如果有两个奇点,则必须从其中一个奇点出发,另一个奇点终止,如果奇点不存在,则可以从任意点出发,最终一定会回到该点

有向图欧拉路线的判断条件:

  1. 最多只能有两个点的入度不等于出度,
  2. 而且最终必须是其中一个点的出度恰好比入度大1(把它作为起点),另一个入度比出度大1(把它作为终点)。
  3. 当然前提条件是:在忽略边的方向后,图必须是连通的。

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

const int MAX = 1000;
const int N = 26;

int vis[N];
int in[N],out[N];
int edge[N][N];
void init() {
	memset(edge,0,sizeof(edge));
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
	memset(vis,0,sizeof(vis));
}

void dfs(int u) {
	vis[u] = true;
	for(int i = 0; i < N; i++) {
		if(edge[u][i] && !vis[i]) {
			dfs(i);
		}
	}
}
bool judge() {
	int num1,num2;
	num1 = num2 = 0;
	for(int i = 0; i < N; i++) {
		if( in[i] != out[i]) {
			if( in[i] - out[i] == 1) {
				num1++;
			}else if(out[i] - in[i] == 1) {
				num2++;
			}else {
				return false;
			}
		}
	}
	if( num1 > 1 && num2 > 1) {	
		return false;
	}else {
		for(int i = 0; i < N; i++) {
			if(in[i]) {
				dfs(i);
				break;
			}
		}
		for(int i = 0; i < N; i++) {
			if(in[i] && !vis[i]) {
				return false;
			}
			if(out[i] && !vis[i]) {
				return false;
			}
		}
		return true;
	}
}
int main() {
	int T;
	char word[MAX];
	while(scanf("%d",&T) != EOF) {
		while( T-- ) {
			init();
			int n;
			scanf("%d",&n);
			for(int i = 0; i < n; i++) {
				scanf("%s",word);
				int len = strlen(word);
				int u = word[0] - 'a';
				int v = word[len-1] - 'a';
				edge[u][v]++;
				in[u]++;
				out[v]++;
			}
			if( judge() ) {
				printf("Ordering is possible.\n");
			}else{
				printf("The door cannot be opened.\n");
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值