剪枝都不需要的dfs水题
不需要储存每个单词,把首字母与尾字母储存好就行了。
可能多组数据输入有点坑
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>#include <algorithm>
using namespace std;
struct word{
char head;
char next;
}words[10000];
int finds[10000];
int flag,num;
void bfs(char heads)
{
int i,j;
if(heads=='m')
{
flag=1;
return;
}
for(i=0;i<num;i++)
{
if(words[i].head==heads&&finds[i]==1)
{
finds[i]=0;
bfs(words[i].next);
finds[i]=1;
if(flag)
{
return;
}
}
}
}
int main()
{
int i,j,w1,len;
char a[105];
num=0;
while(scanf("%s",a)!=EOF)
{
getchar();
if(a[0]=='0')
{
char start;
flag=0;
bfs('b');
if(flag)
{
printf("Yes.\n");
}
else
{
printf("No.\n");
}
num=0;
}
else
{
len=strlen(a);
words[num].head=a[0];
words[num].next=a[len-1];
finds[num]=1;
num++;
}
}
return 0;
}