Play on Words Euler Loop

Source: Internet
Author: User

Description


There is a large number of magnetic plates on every door. everyplate has one word written on it. the plates must be arranged into a sequence in such a way that every word begins with the sameletter as the previous word ends. for example, the word ''acm ''can be followed by the word ''motorola ''. your task is to write acomputer program that will read the list of words and determinewhether it is possible to arrange all of the plates in a sequence (according to the given rule) and consequently to open thedoor.

Input

Output

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

Sample Input

 

Sample output



Turn this question into an Euler Loop problem...
First, use the first letter of each row as the starting position of the directed graph, and the ending letter as the ending position of the directed graph.











#include <stdio.h>#include <string.h>int a[30];int flag[30];int abs(int a){    if(a<0)return -a;    else return a;}int find(int x){    int temp=a[x];    while(temp!=a[temp]){        temp=a[temp];    }    int v;    while(x!=a[x]){        v=a[x];        a[x]=temp;        x=v;    }    return temp;}void u(int x,int y){    x=find(x);    y=find(y);    if(x!=y){        a[x]=y;    }}int main(int argc, char *argv[]){    int t;    int begin,end;    char ch[1001];    scanf("%d",&t);    while(t--){               int n;        int in[30],out[30];        scanf("%d",&n);               memset(flag,0,sizeof(flag));         memset(in,0,sizeof(in));         memset(out,0,sizeof(out));        for(int i=0; i<=25; i++)a[i]=i;        while(n--){                               scanf("%s",ch);            int len=strlen(ch);            begin=ch[0]-'a';            end=ch[len-1]-'a';            flag[begin]=1;            flag[end]=1;            in[end]++;            out[begin]++;            u(begin,end);               }        int s=0;        for(int i=0; i<=25; i++){            if(flag[i]==1 && a[i]==i)s+=1;        }        if(s==1){            int f_in=0,f_out=0;            int k;            for( k=0; k<=25; k++){                if(flag[k]==1){                    if(in[k]==out[k]){                        continue;                    }else if(abs(in[k]-out[k])==1){                        if(in[k]-out[k]<0)f_out++;                        if(in[k]-out[k]>0)f_in++;                        if(f_out>1 || f_in>1)break;                    }else if(abs(in[k]-out[k])>1){                        break;                    }                }            }            if(k==26){                puts("Ordering is possible.");            }else{                puts("The door cannot be opened.");            }        }else{            puts("The door cannot be opened.");        }    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.