HDU ACM 1181 变形课 (广搜BFS + 动态数组vector)-------第一次使用动态数组vector

http://acm.hdu.edu.cn/showproblem.php?pid=1181

题意:给我若干个单词,若单词A的结尾与单词B的开头相同,则表示A能变成B,判断能不能从b开头变成m结尾.

   如: big-got-them

 

第一次使用动态数组vector

View Code
 1 #include <iostream>
 2 #include <vector>
 3 #include <queue>
 4 using namespace std;
 5 const int MAX = 30;
 6 
 7 int main()
 8 {
 9     vector <int> map[MAX];
10     bool used[MAX] = {0};
11     char str[110];
12     while(cin>>str)
13     {
14         
15         int mark = 0;
16         if(str[0] == '0')
17         {
18             queue <int> q;
19             q.push('b' - 'a');
20             used['b' - 'a'] = 1;
21             while(!q.empty())
22             {
23                 int mid = q.front();
24                 q.pop();
25                 int i;
26                 for(i=0;i<map[mid].size();i++)
27                 {
28                     if(!used[map[mid][i]])
29                     {
30                         q.push(map[mid][i]);
31                         used[map[mid][i]] = 1;
32                         if(map[mid][i] == 'm'-'a')
33                         {
34                             mark = 1;
35                             break;
36                         }
37                     }
38                 }
39                 if(mark)
40                 {
41                     break;
42                 }
43             }
44             if(mark)
45             {
46                 cout<<"Yes."<<endl;
47             }
48             else
49             {
50                 cout<<"No."<<endl;
51             }
52             memset(used,0,sizeof(used));
53             int i;
54             for(i=0;i<MAX;i++)
55             {
56                 map[i].clear();
57             }
58         }
59         else
60         {
61             map[str[0] - 'a'].push_back(str[strlen(str) - 1] - 'a');
62         }
63     }
64 }

 

 

 

posted @ 2012-08-30 10:52  zx雄  阅读(294)  评论(0编辑  收藏  举报