HDUOJ----1181 变形课

变形课

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

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
 

宽度搜索

代码如下:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<iostream>
 5 #include<queue>
 6 using namespace std;
 7 const int maxn=10000;
 8 struct nod 
 9 {
10  char st,en;
11 };
12 nod str[maxn+5];
13 bool bfs(int pos ,int step)
14 {
15     queue<nod>cir;
16     nod temp;
17     int i;
18     cir.push(str[pos]);
19     while(!cir.empty())
20     {
21         temp=cir.front();
22         cir.pop();
23         for(i=0;i<step;i++)
24         {
25            if(temp.en=='m')
26             return true;
27            if(temp.en==str[i].st)
28            {
29                if(str[i].en=='m')
30                {
31                    return true;
32                }
33                /*入队*/
34               if(str[i].st=='b'||str[i].en=='b'||str[i].st==str[i].en)
35                    /*不许入队*/ ;
36               else
37                 {
38                   cir.push(str[i]);
39                   str[i].en='0';
40                   str[i].st='0';
41               }
42            }
43         }
44     }
45 return false;
46 }
47 int main()
48 {
49     char s[100];
50     int step=0,i;
51     while(scanf("%s",s)!=EOF)
52     {
53       /*输出结果*/
54     if(*s=='0')
55     {
56      bool tag=false;
57      for(i=0;i<step;i++)
58      {
59          if(str[i].st=='b')
60          {
61              if(bfs(i,step))
62              {         
63                  tag=true;
64                  break;
65              }
66          }
67      }
68      puts(tag?"Yes.":"No.");
69       step=0;
70       memset(str,'0',sizeof(str));
71     }
72     else
73     {
74         str[step].st=*s;
75         str[step++].en=s[strlen(s)-1];
76     }
77 
78     }
79     return 0;
80 }
View Code
原文地址:https://www.cnblogs.com/gongxijun/p/3416218.html