Play on Words POJ - 1386

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

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

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

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.

題意:
要开一个门,n个单词是密码,n个单词中,如果一个单词的首字母和前一个单词
的尾字母相同,并且每个单词都能这么连起來且只用一次,则门可以开,否则不能开。
思路:
直接用dfs找是否所有点为一个集合,如果是,再判断欧拉回路,由于是有向图,
判断奇数点为两个且均为出度比入度大一或者入度比出度大一,或者所有点入度等于出度

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int g[30][30],vis[30];
int in[30],out[30];
int n;
void init()
{
    memset(vis,0,sizeof(vis));
    memset(g,0,sizeof(g));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    return ;
}
void input()
{
    int u,v,len;
    char word[1010];
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",word);
        len=strlen(word);
        u=word[0]-'a'+1;
        v=word[len-1]-'a'+1;
        out[u]++;
        in[v]++;
        g[u][v]=g[v][u]=1;
    }
    return ;
}

void dfs(int i)
{
    int j;
    for(j=1; j<=26; j++)
        if(g[i][j] && !vis[j])  //基图中有无向边(i,j)并且j点还没有被访问过
        {
            vis[j]=1;
            dfs(j);
        }
    return ;
}
int dfs()
{
    int i,count;
    for(count=0,i=1; i<=26; i++)
        if(in[i] || out[i])  //在输入中是有这个点的
            if(!vis[i])     //而且这个点还没有访问过
            {
                vis[i]=1;
                count++;
                dfs(i);
            }

    if(count>1)  //基图不止一个连通分量,也就是不连通的
        return 0;
    else
        return 1;
}

int Euler()
{
    int i;
    int n1,n2;  //n1记录出度把入度大1的点的个数,n2记录入度比出度大1的点的个数
    n1=n2=0;
    for(i=1; i<=26; i++)
    {
        if(out[i]-in[i]==1)  //出度比入度大1
        {
            n1++;
            if(n1>1) break;
            //最多允许一个点的出度比入度大1,如果超过1个就不是欧拉路
        }
        else if(in[i]-out[i]==1) //入度比出度大1
        {
            n2++;
            if(n2>1)
                break; //最多允许一个点的入度比出度大1,如果超过1个就不是欧拉路

        }
        else if( abs(in[i]-out[i])>1 )  //某个点的入度和出度的差值大于1,那不是欧拉路
            break;
    }

    if(i<=26)  //没有完整扫描完整个点集说明中途跳出,不满足欧拉路的要求
        return 0;
    else
        return 1;

}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        input();
        if(!dfs())
        {

            printf("The door cannot be opened.\n");
        }
        else //图是连通的,只要扫描一次所有的点,判断它们的度即可
        {
            if(!Euler())
            {

                printf("The door cannot be opened.\n");
            }
            else
                printf("Ordering is possible.\n");
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值