zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    117. Populating Next Right Pointers in Each Node II
    116. Populating Next Right Pointers in Each Node
    DFS & BFS
    Binary Search
    博客保存
    python强大的正则表达式
    游戏注意的地方
    vim使用
    下一步的
    lua的动态特性
  • 原文地址:https://www.cnblogs.com/sola1994/p/4681678.html
Copyright © 2011-2022 走看看