您的位置:首页 > 其它

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

2012-08-30 10:52 375 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1181

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

   如: big-got-them

第一次使用动态数组vector

View Code

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int MAX = 30;

int main()
{
vector <int> map[MAX];
bool used[MAX] = {0};
char str[110];
while(cin>>str)
{

int mark = 0;
if(str[0] == '0')
{
queue <int> q;
q.push('b' - 'a');
used['b' - 'a'] = 1;
while(!q.empty())
{
int mid = q.front();
q.pop();
int i;
for(i=0;i<map[mid].size();i++)
{
if(!used[map[mid][i]])
{
q.push(map[mid][i]);
used[map[mid][i]] = 1;
if(map[mid][i] == 'm'-'a')
{
mark = 1;
break;
}
}
}
if(mark)
{
break;
}
}
if(mark)
{
cout<<"Yes."<<endl;
}
else
{
cout<<"No."<<endl;
}
memset(used,0,sizeof(used));
int i;
for(i=0;i<MAX;i++)
{
map[i].clear();
}
}
else
{
map[str[0] - 'a'].push_back(str[strlen(str) - 1] - 'a');
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: