hdu 1181 变形课 (bfs)

 

 

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
char str[100];
int mat[30][30];
int vis[30][30];
int ok;
bool isok(int now,int next)
{
    if(mat[now][next]==0||vis[now][next]==1) return false;
    return true;
}
void bfs()
{
    int i,j,k;
    queue<int> q;
    q.push('b'-'a');
    while(!q.empty())
    {
       int now,next;
       now=q.front();
       q.pop();
       if(now=='m'-'a')
       {
           ok=1;
           return ;
       }
       for(i=0;i<26;i++)
       {
           next=i;
           if(isok(now,next))
           {
               if(now=='m'-'a')
               {
                 ok=1;
                 return ;
               }
               vis[now][next]=1;
               q.push(next);
           }
       }

    }
}

int main()
{
    int cnt;
    int n;
    int i,j,k;
    int f,t;
    while(scanf("%s",str)!=EOF)
    {
        if(strcmp(str,"0")==0)
        {
            printf("No.\n");continue;
        }
        ok=0;
        memset(mat,0,sizeof(mat));
        memset(vis,0,sizeof(vis));
        f=str[0]-'a';
        t=str[strlen(str)-1]-'a';
        mat[f][t]=1;
        while(1)
        {
            scanf("%s",str);
            if(strcmp(str,"0")==0) break;
            f=str[0]-'a';
            t=str[strlen(str)-1]-'a';
            mat[f][t]=1;
        }
        bfs();
        if(ok) printf("Yes.\n");
        else printf("No.\n");
    }
    return 0;
}

 

posted @ 2015-07-28 00:53  sola94  阅读(190)  评论(0编辑  收藏  举报