hdu 1181 以b开头m结尾的咒语 (DFS)

咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体
现在要将一个B(ball)变成一个M(Mouse),
比如 "big-got-them".

Sample Input
so
soon
river
goes
them
got
moon
begin
big
0

Sample Output
Yes.

 1 # include <cstdio>
 2 # include <cmath>
 3 # include <iostream>
 4 # include <cstring>
 5 # include <algorithm>
 6 using namespace std ;
 7 
 8 int l = 0,flag = 0;
 9 
10 struct Node
11 {
12     char head,end;
13 } c[10000];
14 int vis[10000];
15 
16 void dfs(char ch)
17 {
18     int i;
19     if(ch == 'm')
20         flag = 1;
21     if(flag)
22         return;
23     for(i = 0; i<l; i++)
24     {
25         if(vis[i])
26             continue;
27         if(c[i].head == ch)
28         {
29             vis[i] = 1;
30             dfs(c[i].end);
31             vis[i] = 0;
32         }
33     }
34 }
35 
36 int main()
37 {
38     //freopen("in.txt","r",stdin) ;
39     char s[100];
40     int i;
41     while(~scanf("%s",s))
42     {
43         if(!strcmp(s,"0"))
44             continue;
45         l = 0;
46         c[l].head = s[0];
47         c[l].end = s[strlen(s)-1];
48         l++;
49         while(scanf("%s",s))
50         {
51             if (strcmp(s,"0") == 0)
52                 break ;
53             c[l].head = s[0];
54             c[l].end = s[strlen(s)-1];
55             l++;
56         }
57         flag = 0;
58         for(i = 0; i<l; i++)
59         {
60             if(c[i].head == 'b')
61             {
62                 memset(vis,0,sizeof(vis));
63                 vis[i] = 1;
64                 dfs(c[i].end);
65             }
66             if(flag)
67                 break;
68         }
69         if(flag)
70             printf("Yes.\n");
71         else
72             printf("No.\n");
73     }
74 
75     return 0;
76 }
View Code

 

posted @ 2015-05-16 20:01  __Meng  阅读(483)  评论(0编辑  收藏  举报