首页 > 代码库 > Play on Words UVA - 10129

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 ``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.

 

题目大意翻译:

 

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

 

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

一样。例如, motorola的后面可以接上acm。 

 

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

 

样例输入:

 

3
2
acm
ibm
3
acm
malform
mouse
2
ok
ok

 

 

样例输出:

 

The door cannot be opened.
Ordering is possible.
The door cannot be opened.
开始用dfs写的,结果因为数据太大,时间超限,然后再网上搜了下才知道是用欧拉回路加dfs做的
这是我的dfs代码
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
char mapn[100010][1010];
int flag,n,vis[100010];
void dfs(char c,int a)
{
    if( a == n)
    {
        flag = 1;
        return;
    }
    for(int i=0;i<n;i++)
    {
        if(mapn[i][0] == c && !vis[i])
        {
            vis[i] = 1;
            dfs(mapn[i][strlen(mapn[i])-1],++a);
            vis[i] = 0;
        }
    }
    return;
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        flag = 0;
        memset(vis,0,sizeof(vis));
        memset(mapn,0,sizeof(mapn));
        cin >> n;
        for(int i=0;i<n;i++)
            cin >> mapn[i];
        for(int i=0;i<n;i++)
        {
            vis[i] = 1;
            dfs(mapn[i][strlen(mapn[i])-1],1);
            vis[i] = 0;
        }
        if(flag)
            cout << "Ordering is possible." << endl;
        else cout << "The door cannot be opened." << endl;
    }
    return 0;
}

下面这个是用欧拉回路加dfs做的

#include<cstring>
#include<iostream>
#include<cstdio>
#include<cmath>
#define MAXN 100
using namespace std;
int vis[MAXN],G[MAXN][MAXN],N, T, in[MAXN],out[MAXN];
void dfs(int u)//只用dfs会时间超限
{
    vis[u] = true;
    for(int i=0; i<MAXN; ++i)
    {
        if(G[u][i] && !vis[i])
            dfs(i);
    }
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        memset(G, 0, sizeof(G));
        memset(in, 0, sizeof(in));
        memset(out, 0, sizeof(out));
        char str[1005];
        scanf("%d",&N);
        for(int i=0; i<N; ++i)
        {
            scanf("%s", str);
            ++G[str[0]-a][str[strlen(str)-1]-a];//存下每个单词的开头结尾好搜索
            ++out[str[0]-a];//出度
            ++in[str[strlen(str)-1]-a];//入度
        }
        bool flag=true;
        int num1=0, num2=0;
        for(int i=0; i<MAXN; ++i)
        {
            if(!flag)
                break;
            /*对于有向图, 则必须其中一个点的出度恰好比入度大1,
             另一个的入度比出度大。
                如果奇点数不存在的话,
                则可以从任意点出发,最终一定会回到该点(成为欧拉回路)。*/
            if(in[i]!=out[i])
            {
                if(in[i]==out[i]+1)
                {
                    ++num1;
                }
                else if(out[i]==in[i]+1)
                {
                    ++num2;
                }
                else
                {
                    flag=false;
                    break;
                }
            }
        }
        if(num1 && num2 && num1+num2>2)
            flag=false;
        if(flag)
        {
            memset(vis, 0, sizeof(vis));
            for(int i=0; i<MAXN; ++i)
                if(out[i])
                {
                    dfs(i);
                    break;
                }
            //搜索判断是否构成回路
            bool flag2=true;
            for(int i=0; i<MAXN; ++i)
            {
                if(in[i] && !vis[i])
                {
                    flag2=false;
                    break;
                }
                if(out[i] && !vis[i])
                {
                    flag2=false;
                    break;
                }
            }
            if(flag2) printf("Ordering is possible.\n");
            else printf("The door cannot be opened.\n");
        }
        else
        {
            printf("The door cannot be opened.\n");
        }
    }
    return 0;
}

 

Play on Words UVA - 10129