• 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.
    ");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.
    ");
            else printf("No.
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    关于区间数颜色的主席树解决
    1020考试总结
    QR算法
    新的征程
    端点星2020.12.2联赛
    自我介绍&友链
    3个搜索
    搜索格式这样写
    T107073 归并排序
    还有这个题
  • 原文地址:https://www.cnblogs.com/sola1994/p/4681678.html
Copyright © 2020-2023  润新知