`
473687880
  • 浏览: 535444 次
文章分类
社区版块
存档分类
最新评论

Play on Words UVA10129

 
阅读更多

Play on Words

Some of the secret doors contain avery 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 alarge 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 asequence (according to the given rule) and consequently to open the door.

Input Specification

The input consists ofTtest cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing asingle integer numberNthat indicates the number of plates (1 <= N <= 100000). Then exactlyNlines follow, each containing asingle 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 asequence 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 thesentence "Ordering is possible.". Otherwise, output thesentence "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.


又是一道可以建模成欧拉图的问题,看题目第一感觉就是搜索或者递归,但是没有想到建模成欧拉图,对欧拉的图的判断还是太不清楚了,要多练练。

#include<iostream>
#include<cstring>
#include<string>

using namespace std;

int tag,m;
int in[30],out[30];
int grid[30][30],vis[30][30];

void dfs(int k)
{
    for(int i=0;i<26;i++)
    {
        if(grid[k][i]&&vis[k][i]==0)
        {
            vis[k][i]=1;
            dfs(i);
        }
    }
}

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(grid,0,sizeof(grid));
        memset(vis,0,sizeof(vis));
        int i,j;
        string str;
        cin>>m;
        for(i=0;i<m;i++)
        {
            cin>>str;
            int x=str[0]-'a';
            int y=str[str.size()-1]-'a';
            out[x]++;
            in[y]++;
            grid[x][y]=1;
            grid[y][x]=1;
        }
        tag=1;
        if(m>1)
        {
            for(i=0;i<26;i++)
            {
                if(in[i]-out[i]>1||out[i]-in[i]>1)
                {
                    tag=0;
                    break;
                }
            }
            for(i=0;i<26;i++)
            {
                if(out[i]>0)
                {
                    dfs(i);
                    break;
                }
            }
            for(i=0;i<26;i++)
            {
                for(j=0;j<26;j++)
                {
                    if(grid[i][j]&&vis[i][j]==0)
                    {
                        tag=0;
                    }
                }
            }
        }
        if(tag) cout<<"Ordering is possible."<<endl;
        else cout<<"The door cannot be opened."<<endl;
    }
    return 0;
}


分享到:
评论

相关推荐

    Words used in play scriptsPPT教案学习.pptx

    Words used in play scriptsPPT教案学习.pptx

    苹果carplay开发资料

    ### 知识点一:CarPlay概述 CarPlay是苹果公司推出的一种智能车载系统,它的目标是提供一种更加安全、便捷的方式来使用iPhone。通过CarPlay,用户能够将iPhone的功能与车载信息娱乐系统相结合,从而在驾驶时减少...

    carplay_ios_carplay_apple_

    CarPlay是苹果公司推出的一种车载信息系统,旨在将iOS设备与汽车内置系统无缝集成,提供安全、便捷的驾驶体验。这个系统允许用户通过汽车的触摸屏或语音控制来访问iPhone的应用,如地图、音乐、电话、短信等,同时...

    CarPlay_apple_carplay_iap2

    Apple CarPlay是苹果公司推出的一款车载智能交互系统,它允许用户通过汽车的内置显示屏和控制键,或Siri语音控制来安全地使用iPhone进行导航、音乐播放、电话通话、消息阅读以及众多第三方应用。iap2,全称为In-...

    CarPlay Communication Plug-in R14G17_carplay_Carplayplugin

    CarPlay Communication Plug-in R14G17_carplay_Carplayplugin是一款专为苹果CarPlay系统设计的通信插件,其主要功能是增强车辆与iPhone之间的连接性和交互性。这款插件是苹果公司针对车载信息系统的一项重要更新,...

    PlayOn-开源

    PlayOn开源项目是一个致力于提供简单、直观的方式,让玩家能够轻松告知朋友们他们当前是否愿意参与在线游戏的平台。这个小巧的实用程序具有开源的特点,意味着它的源代码是公开的,允许用户、开发者和社区自由查看、...

    xindawn-windows-airplay-master.zip_Air Media Serve_airplay_airpl

    《Windows平台上的AirPlay服务端程序开发详解》 在当今数字化时代,无线音频和视频传输技术日益普及,其中AirPlay是苹果公司推出的一种强大的无线媒体流协议,它允许用户将iPhone、iPad或Mac等设备上的媒体内容无线...

    WINDOWS上面AirPlay Receiver for PC/MAC

    LonelyScreen is a AirPlay receiver for Windows and MAC. It is like an Apple TV running on your desktop. You can cast anying from your iPhone or iPad to your computer screen just like a Apple TV. To ...

    play框架jar包

    Play框架是一款基于Java和Scala的开源Web应用框架,它遵循模型-视图-控制器(MVC)架构模式,旨在提供高效、简洁且快乐的开发体验。这个资源包含了一系列用于搭建Play框架的jar包,这些jar包是开发Play应用程序所...

    google_play_services最新

    谷歌Play服务(Google Play Services)是Android操作系统中的一个核心组件,它为开发者提供了与Google服务集成的API,包括Google Maps、Google+、Google Drive、Firebase等。这些服务使得应用程序能够利用谷歌的基础...

    wuziqi.rar_play wuziqi_play wuziqi online_wuziqi how to pl_wuziq

    标签 "play_wuziqi"、"play_wuziqi_online"、"wuziqi_how_to_pl" 和 "wuziqi_online_play" 强调了游戏的可玩性和在线功能,意味着该程序可能支持单机模式以及网络对战。 在压缩包内的文件中,我们看到以下几个文件...

    play framework api,play! framework api,play api

    Play Framework 是一个开源的Web应用框架,用于构建现代、高性能的Java和Scala应用程序。它采用模型-视图-控制器(MVC)架构模式,并且强调简洁的代码和开发的即时反馈。Play Framework API 是开发者使用该框架进行...

    Playframework 1.2.7 sdk zip包 play1.2.7.zip

    Playframework 1.2.7 是一个开源的Java和Scala Web应用框架,它采用模型-视图-控制器(MVC)架构模式,并且是基于事件驱动的,这使得开发过程更加高效和简洁。这个zip包"play1.2.7.zip"包含了Playframework的SDK,...

    Rules of Play: Game Design Fundamentals

    Chapter 12 - Rules on Three Levels Chapter 13 - The Rules of Digital Games Chapter 14 - Games as Emergent Systems Chapter 15 - Games as Systems of Uncertainty Chapter 16 - Games as ...

    ios9 airplay源码

    《iOS9 AirPlay源码解析》 iOS9 AirPlay源码是针对苹果设备在iOS9及以上版本中实现AirPlay功能的编程代码。AirPlay是由Apple公司开发的一种无线技术,允许用户将音频、视频、照片和镜像从一个设备无线传输到其他...

    play-services-plugins,插件来帮助使用google play服务sdk。.zip

    在Android开发中,Google Play服务SDK是一个至关重要的组件,它提供了许多功能,如地图、推送通知、游戏服务、身份验证等。"play-services-plugins"是一个开源项目,旨在简化与Google Play服务集成的过程,帮助...

    win-airplay

    对于苹果用户来说,AirPlay一定是一项令人印象深刻的技术,可以实现苹果不同设备间的内容投放,例如,iPhone、iPad中的内容可以通过AirPlay在Mac的屏幕上投放出来,不仅可以增强个人的视觉体验,还十分有利于群体间...

    google play service 最新版 firebase sdk 9.2

    google play service最新版本,基于firebase的sdk,9.2.0.包含了 全部需要的jar,另外还有aar版本 play-services-ads-9.2.0.jar play-services-analytics-9.2.0.jar play-services-analytics-impl-9.2.0.jar play-...

    com.google.android.gms:play-gms:play-services:15.0.1

    解决: Could not find play-services-basement.aar (com.google.android.gms:play-services-basement:15.0.1). Searched in the following locations: ...

    play-google, Play-Google可以快速地搭建一台Google搜索服务器。.zip

    【Play-Google:快速搭建Google搜索服务器】 Play-Google 是一个开源项目,旨在帮助用户便捷地建立自己的Google搜索服务器。这个项目的核心理念是利用Google的公开搜索API,结合开源技术,为用户提供私有的、定制化...

Global site tag (gtag.js) - Google Analytics