Play on Words UVA - 10129(哈密顿)

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 `

m

otorola

'.

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

The input consists of

T

test cases. The number of them (

T

) is given on the rst line of the input le.

Each test case begins with a line containing a single integer number

N

that indicates the number of

plates (1

N

100000). Then exactly

N

lines 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

Your program has to determine whether it is possible to arrange all the plates in a sequence such that

the rst 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

3

2

acm

ibm

3

acm

malform

mouse

2

ok

ok

Sample Output

The door cannot be opened.

Ordering is possible.

The door cannot be opened.

这道题就是一道哈密顿图的模板题,注意判断图的连通性 和 除起始点和终止点外出度等于入度。

对哈密顿通路不了解的同学请点击此链接看看https://blog.csdn.net/lh__huahuan/article/details/62235476

在做这道题的时候一定要注意 “只有一个顶点的环也是符合要求的”,我看了其他人写的题解,有很多人并没有考虑到这点,不知道当初是怎么AC的。。。。

核心:以每个字符串的首和尾字符为图的顶点,字符串为边。

AC代码(dfs判断连通性):

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int maxn = 1005;
char s[maxn];
int g[maxn][maxn],in[maxn],out[maxn],vis[maxn];
//in代表入度,out代表出度,vis代表是否访问过,g[i][j]是存i到j的边。
void dfs(int u){
    vis[u] = 1;
    for(int i = 0;i < 26;i++){
        if(!vis[i] && g[u][i])//如果未被访问过,且有有向边,继续dfs
            dfs(i);
    }
}
int main()
{
    int t,n;
    cin>>t;
    while(t--){
        memset(g,0,sizeof(g));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(vis,0,sizeof(vis));
        cin>>n;
        for(int i = 0;i < n;i++){
            cin>>s;
            int f = s[0]-'a';//通过ASC码将字符转换为方便遍历的整数类型
            int l = s[strlen(s)-1] - 'a';
            in[l]++;
            out[f]++;//重点:这里千万不能写反了,如果不知道为什么的自己画图看看。
            g[f][l]++;
        }

        bool flag = true;
        int head = 0,last = 0;
        for(int i = 0;i < 26;i++){
            if(in[i] == out[i]) continue;
            if(out[i] == in[i]+1) head++;
            else if(in[i] == out[i]+1) last++;
            else {flag = false;break;}//这里代表除了头和尾外出现了奇度顶点。
        }
        if(head && last && head+last>2) flag = false;//连通度大于1

        if(flag){
            for(int i = 0;i < 26;i++){
                if(out[i] == in[i]+1 || (head == 0&&last==0)){
                    dfs(i);
                    break;
                }
            }
            bool connect = true;
            for(int i = 0;i < 26;i++){
                if(!vis[i] && in[i]){
                    connect = false;
                    break;
                }
                if(!vis[i] && out[i]){
                    connect = false;
                    break;
                }
            }
            if(connect) cout<<"Ordering is possible."<<endl;
            else cout<<"The door cannot be opened."<<endl;
        }
        else{
            cout<<"The door cannot be opened."<<endl;
        }

    }
    return 0;
}
发布了82 篇原创文章 · 获赞 265 · 访问量 58万+

猜你喜欢

转载自blog.csdn.net/zhiyeegao/article/details/84404789