杭电1181

DFS深度优先搜索

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int a[26][26];
 5 char s[1000];
 6 int ans=0;
 7 
 8 int lenth(char s[]);
 9 void dfs(int);
10 
11 int main()
12 {
13     int len;
14     while(~scanf("%s",s))
15     {
16         if(s[0]=='0')
17         {
18             dfs(1);
19             memset(a,0,sizeof a);
20             if(ans)
21                 printf("Yes.
");
22             else
23                 printf("No.
");
24             ans=0;
25             continue;
26         }
27         len=lenth(s);
28         a[s[0]-'a'][s[len-1]-'a']=1;
29     }
30 }
31 
32 int lenth(char s[])
33 {
34     int x=0;
35     while(s[x]!='')
36         ++x;
37     return x;
38 }
39 
40 void dfs(int x)
41 {
42     int i;
43     for(i=0; i<26; ++i)
44     {
45         if(a[x][12])    //12即m
46         {
47             ans=1;
48             return;
49         }
50         if(a[x][i])
51         {
52             a[x][i]=0;
53             dfs(i);
54             a[x][i]=1;
55         }
56     }
57 }
原文地址:https://www.cnblogs.com/qq188380780/p/6118004.html