DFS training - Transfiguration - solving report

Transfiguration

Subject description:

Uh ... deformation class Harry ran into a little trouble, because he did not like Hermione can remember all the spells and casual baseball will become a hedgehog or something, but he found a unified law of deformation mantra: if the spell begins with a b end of a word, its role is exactly the object a becomes B object.
Harry has all the spells he would have become a column of the table, he wants you to help calculate whether he could complete teacher's job, a B (ball) into a M (Mouse), you know, if he can not complete it, he had to ask Hermione, and forced to listen a lot to learn the truth.

Input:

Multiple sets of test data. Multiple rows in each group, one word per line, includes only lowercase letters, all the spells Harry will Numeral 0 indicates the end of a set of inputs.

Output:

If Harry could finish his homework, outputs "Yes.", Otherwise output "No." (Do not ignore the full stop)

Sampe Input:

Here Insert Picture Description

Sample Output:

Here Insert Picture Description

Analysis of ideas:

This question is equivalent to the word Solitaire same, but the subject of the request is from the beginning with the letter b, so we must first identify the beginning of the letter b, record them, and then ask the end of the letter is the letter m to meet the conditions. To enter this question did not say how many, so use vector structure to write, but I can not write, uncomfortable, can only expand the array. Whether we can meet the conditions, actually we had the okay. This question did not say setting input conditions, it is necessary to be continuous output, there is more trouble, ╮ (╯ ▽ ╰) ╭.

AC Code:

When used: 46ms


#include<cstdio>
#include<string.h>
char c1[10000][20];//记录单词 
int d[10000];//记录b开头单词所出现的序号 
int e[10000];//标记是否用过该单词 
int g=0;//记录有多少个单词 
int flag,flag1;//flag标记是否能够接龙成功,这个flag1也和flag差不多 
void LemonDFS(char c2)
{
	int i;
	if(flag)//这里是为了缩短运行时间,一旦达到要求了,后面就不用在继续DFS 
	{
		return;
	}
	for(i=0;i<g;i++)//分三种情况去看 
	{
		if(c1[i][strlen(c1[i])-1]=='m'&& c2=='m')//如果第一个找到m开头,就退出 
		{
			flag=1;
			return;
		}
		if(c2==c1[i][0] && c1[i][strlen(c1[i])-1]=='m' && e[i]!=1)//如果找到 m开头就退出 
		{
			flag=1;
			return;
		}
		if(c2==c1[i][0] && e[i]!=1)//如果接龙可以,并且没使用过该单词,就DFS下去 
		{
			e[i]=1;//标记用过该数字 
			LemonDFS(c1[i][strlen(c1[i])-1]);
			if(flag)//剪枝,缩短时间 
			return;
			e[i]=0;
		}
	}
}
int main()
{
	int b=0,c=0;
	while(scanf("%s",c1[g])!=EOF)//为了持续输入,这里就吐了 
	{ 
		flag=0;
		flag1=0;
		if(c1[g][0]=='0')
		{
			for(b=0;b<c;b++)//记录单词 
	{
		e[d[b]]=1;//记录 
		LemonDFS(c1[d[b]][strlen(c1[d[b]])-1]);//传b字母进去 
		if(flag)
		{
			printf("Yes.\n");
			flag1=1;
			break;
		}
		e[d[b]]=0;
	} 
	if(!flag1)
	printf("No.\n");
	memset(c1,0,sizeof(c1));
	memset(e,0,sizeof(g));
	memset(d,0,sizeof(d));
	g=0;
	continue;
		}
		if(c1[g][0]=='b')//记录b开头的单词 
		{
			d[c++]=g;
		}
		g++;
	}
}
Published 49 original articles · won praise 6 · views 10000 +

Guess you like

Origin blog.csdn.net/xiaosuC/article/details/104247037
Recommended