zoukankan      html  css  js  c++  java
  • hdu 1181 变形课

    变形课

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
    Total Submission(s): 21512    Accepted Submission(s): 7761


    Problem Description
    呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体. 
    Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
     
    Input
    测试数据有多组。每组有多行,每行一个单词,仅包括小写字母,是Harry所会的所有咒语.数字0表示一组输入结束.
     
    Output
    如果Harry可以完成他的作业,就输出"Yes.",否则就输出"No."(不要忽略了句号)
     
    Sample Input
    so soon river goes them got moon begin big 0
     
    Sample Output
    Yes.
    Hint
    Hint
    Harry 可以念这个咒语:"big-got-them".
     
    Source
    #include<iostream>
    #include<stdio.h>
    #include<queue>
    #include<string>
    #include<string.h>
    #define inf 0x7fffffff
    using namespace std;
    int ma[28][28];
    bool vis[27]= {false};
    bool bfs(int x)
    {
        queue<int> q;
        while(!q.empty()) q.pop();
        q.push(x);
        int now=x;
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            vis[now]=true;
            for(int i=0; i<26; i++)
            {
                if(ma[now][i]!=inf&&!vis[i])
                {
                    int next=i;
                    if(next==('m'-'a')) return true;
                    q.push(next);
                }
            }
        }
        return false;
    
    }
    int main()
    {
        for(int i=0; i<26; i++)
            for(int j=0; j<26; j++)
                ma[i][j]=inf;
        char a[200];
        while(~scanf("%s",a))
        {
            if(!(strlen(a)==1&&a[0]=='0'))
            {
                int len=strlen(a);
                ma[a[0]-'a'][a[len-1]-'a']=1;
            }
            else
            {
                /*cout<<1<<endl;
                for(int i=0; i<26; i++)
                {
                    for(int j=0; j<26; j++)
                        cout<<ma[i][j]<<" ";
                    cout<<endl;
                }*/
                if(bfs(1))
                {
                    printf("Yes.
    ");
                }
                else
                {
                    printf("No.
    ");
                }
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    leetcode 33. Search in Rotated Sorted Array
    leetcode 32. Longest Valid Parentheses
    leetcode 28. Implement strStr()
    leetcode 27. Remove Element
    leetcode 26. Remove Duplicates from Sorted Array
    leetcode 24. Swap Nodes in Pairs
    leetcode 22. Generate Parentheses
    树莓派的频率管理和热控制
    sql执行insert插入一条记录同时获取刚插入的id
    全程直播个人博客重构过程,采用springboot+dubbo+jpa技术栈。
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5716204.html
Copyright © 2011-2022 走看看