码迷,mamicode.com
首页 > 其他好文 > 详细

【搜索】HDU1181:变形课

时间:2018-02-10 23:21:59      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:图片   const   map   init   stdio.h   otto   gif   cli   一个   

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".

 

技术分享图片
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<queue>
 4 #include<string>
 5 #include<string.h>
 6 using namespace std;
 7 const int INF = 0x7fffffff;
 8 int mapp[30][30];
 9 char str[200];
10 bool vis[30]={false};
11 void init()
12 {
13     for(int i=0;i<26;i++)
14     {
15         for(int j=0;j<26;j++)
16             mapp[i][j]=INF;
17     }
18 }
19 bool bfs(int x)
20 {
21     queue<int> q;
22     while(!q.empty())
23         q.pop();
24     q.push(x);
25     int now=x;
26     while(!q.empty())
27     {
28         now=q.front();
29         q.pop();
30         vis[now]=true;
31         for(int i=0;i<26;i++)
32         {
33             if(mapp[now][i]!=INF&&!vis[i])
34             {
35                 int next=i;
36                 if(next==(m-a))
37                     return true;
38                 q.push(next);
39             }
40         }
41     }
42     return false;
43 }
44 int main()
45 {
46     init();
47     while(scanf("%s",str)!=EOF)
48     {
49         if(strlen(str)!=1&&str[0]!=0)
50         {
51             int len=strlen(str);
52             mapp[str[0]-a][str[len-1]-a]=1;
53         }
54         else
55         {
56             bool flag=bfs(1);
57             if(flag)
58             {
59                 cout << "Yes." << endl;
60             }
61             else
62             {
63                 cout << "No." <<endl;
64             }
65         }
66     }
67     return 0;
68 }
View Code

【搜索】HDU1181:变形课

标签:图片   const   map   init   stdio.h   otto   gif   cli   一个   

原文地址:https://www.cnblogs.com/SoulSecret/p/8439941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!