hdu 1181 深搜

中文题

深搜

许久没写鸟,卡在输入问题上...

 

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 bool flg;
 5 int n;
 6 char str[5000][2];
 7 bool used[5000];
 8 void dfs(int p)
 9 {
10     int i;
11     if (p >=n || flg) return;
12     if (str[p][1] == 'm')    
13     {
14         flg = true;
15         return;
16     }
17     for (i=0; i<n; i++)
18         if (!used[i] && str[i][0] == str[p][1])
19         {
20             used[i] = true;
21             dfs(i);
22             used[i] = false;
23 
24         }
25 }
26 int main()
27 {
28 
29     int i;
30     string tmp;//="12345";
31     //cout<<tmp.back()<<endl;
32     while (cin>>tmp && tmp[0] != '0')
33     {
34         i=0;
35         flg = false;
36         memset(used, false, sizeof(used));
37         str[i][0] = tmp[0];
38         str[i++][1] = tmp[tmp.length()-1];
39         while (cin>>tmp && tmp[0] != '0') 
40         {
41             str[i][0] = tmp[0];
42             str[i++][1] = tmp[tmp.length()-1];
43         }
44         n = i;
45         for (i=0; i<n; i++)
46             if (!flg && str[i][0] == 'b')
47             {
48                 used[i] = true;
49                 dfs(i);
50                 used[i] = false;
51             }
52             if (flg)
53                 cout<<"Yes."<<endl;
54             else cout<<"No."<<endl;
55     }
56 }
View Code

 

posted @ 2013-07-18 15:36  旅行的蜗牛  阅读(137)  评论(0编辑  收藏  举报