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

时间:2022-01-25 05:50:36

咒语是以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.

 # include <cstdio>
# include <cmath>
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std ; int l = ,flag = ; struct Node
{
char head,end;
} c[];
int vis[]; void dfs(char ch)
{
int i;
if(ch == 'm')
flag = ;
if(flag)
return;
for(i = ; i<l; i++)
{
if(vis[i])
continue;
if(c[i].head == ch)
{
vis[i] = ;
dfs(c[i].end);
vis[i] = ;
}
}
} int main()
{
//freopen("in.txt","r",stdin) ;
char s[];
int i;
while(~scanf("%s",s))
{
if(!strcmp(s,""))
continue;
l = ;
c[l].head = s[];
c[l].end = s[strlen(s)-];
l++;
while(scanf("%s",s))
{
if (strcmp(s,"") == )
break ;
c[l].head = s[];
c[l].end = s[strlen(s)-];
l++;
}
flag = ;
for(i = ; i<l; i++)
{
if(c[i].head == 'b')
{
memset(vis,,sizeof(vis));
vis[i] = ;
dfs(c[i].end);
}
if(flag)
break;
}
if(flag)
printf("Yes.\n");
else
printf("No.\n");
} return ;
}