这题有三种方法;分别是 深搜 广搜 查并集;
查并集的具体方法 见 :http://blog.youkuaiyun.com/free_shy/article/details/8665325
< 一 > :深搜:
//分析:用深搜 用一个结构体 记录每个单词的 首尾字母;
//不断用上一个的单词的尾字母 与下一个单词的首字母对比;;
// 知道找到符合条件的;
// 如果单个字母要作为单词的话 这个代码 就不能AC, 所以 这题并没有考虑这种情况;
//这题不需要“回溯”;如果第一次不符合;下一次再用到这个字母 就仍不符合;就没有意义;
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int k;
bool first;
struct node//定义结构体;
{
char st;// 首字母
char en;//为字母;
}peo[1000];
bool dfs(char ch)
{
if(first)//如果first 为真 就返回。它的上一循环 再进行下一次循环 也会结束;减少时间;
return true;
if(ch == 'm')
{
first = true;
return true;
}
for(int i = 0; i < k; i++)
{
if(peo[i].st != '0' && peo[i].st == ch)
{
peo[i].st = '0';
dfs(peo[i].en);
}
}
}
int main()
{
string str;
while(cin >> str)
{
k = 0;
first = false;
while(str != "0")
{
int len = str.length();
peo[k].st = str[0];
peo[k].en = str[len-1];
k++;
cin >> str;
}
dfs('b');//从“b”开始
if(first)
{
printf("Yes.\n");
}
else
printf("No.\n");
}
}
< 二 > 查并集:
// 用查并集的方法做得;将每个单词的首尾字母分别放在两个数组中,然后用查并集的方法
//就行编排,最后在用查并集的 find 函数,找到 b 与 m的关系;
// 查并集主要是路径压缩,以减少时间;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
using namespace std;
int tot1[1000];//记录单词的首字母;
int tot2[1000];//记录单词的为字母;
int make[30], b;
int find(int x)
{
if(x != make[x])//一直找到x的最上面的数;
{
make[x] = find(make[x]);
}
return make[x];
}
void set(int x, int y)
{
int f1 = find(x);
int f2 = find(y);
if(f1!=f2)
make[f1] = f2;//将f1连接在f2的下面;
}
int set_find(int a)//最后在查一下是否 符合条件;
{
if(b == 1)
return 1;
if(a == 13)
{
b = 1;
return 1;
}
if(a != make[a])
make[a] = set_find(make[a]);
}
int main()
{
string str;
while(cin >> str)
{
int k = 0;
while(str != "0")
{
int len = str.length();
tot1[k] = str[0] - 'a' + 1;
tot2[k] = str[len-1] - 'a' + 1;
k++;
cin >> str;
}
for(int i = 1; i <= 26; i++)//将它们初始化;
{
make[i] = i;
}
for(int i = 0; i < k; i++)//开始查并集;
{
set(tot1[i], tot2[i]);
}
b = 0;
set_find(2);//检查是否符合条件;
if(b == 1)
printf("Yes.\n");
else
printf("No.\n");
}
}
< 三 >:广搜
//这是用广搜的方法;
//用一个结构体将单词的首尾 放入;为深搜做准备;
//用一个for循环找首字母为‘b’的;如果进行深搜 得不到结果,就继续找另一个‘b’;如果得到就跳出;
//在bfs函数中首先进行初始化;就是在主函数中找的的首字母为b的 进行;
//将temp压入队列;
//在while()中找到所有首字母为上一个元素的为字母的 元素 并压入队列;
//全部压入队列后,在从队列中找顶栈的元素;判断是否它的尾字母为m不 是就跳出 不是继续找
//所有的元素的首字母为上一个元素的尾字母 并压入队列;就这样一层一层的找,直到找到或没有
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
bool first;
int k;
struct zm
{
char st;
char en;
}peo[1000];
bool bfs(int n)
{
queue<zm>q;
//if(first)
// return true;
//if(ch == '')
zm temp, type;
temp.st = peo[n].st;
temp.en = peo[n].en;
peo[n].st = '0';
q.push(temp);
while(!q.empty())
{
temp = q.front();
q.pop();
if(temp.en == 'm')
{
first = true;
break;
}
type = temp;
for(int i = 0; i < k; i++)
{
if(peo[i].st != '0' && peo[i].st == temp.en)
{
type.st = peo[i].st;
type.en = peo[i].en;
peo[i].st = '0';
q.push(type);
}
}
}
if(first)
return true;
return false;
}
int main()
{
string str;
while(cin >> str)
{
first = false;
k = 0;
while(str != "0")
{
int len = str.length();
peo[k].st = str[0];
peo[k].en = str[len-1];
k++;
cin >> str;
}
for(int i = 0; i < k; i++)
{
if(peo[i].st == 'b')
{
first = bfs(i);
if(first)
break;
}
}
if(first)
printf("Yes.\n");
else
printf("No.\n");
}
}