HDU 1181 变形课

题目地址:点击打开链接

思路:好题

AC代码:

#include<stdio.h>
#include<string.h>
int visit[30];
int a[30][30];
char lol[10000];
int flag;
void dfs(int n)
{
	int j;
	if(n == 'm' - 'a')
	{
		flag = 1;
		return;
	}
	for(j=0; j<26; j++)
	{
		if(a[n][j] == 1 && !visit[j])//必须加一个判断,不然就死循环了
		//if(a[n][j] == 1)
		{
			visit[j] = 1;
			dfs(j);
		}
	}
}

int main()
{
	int len;
	while(scanf("%s",lol) && lol[0] != '0')
	{
		flag = 0;
		memset(visit,0,sizeof(visit));
		memset(a,0,sizeof(a));
		len = strlen(lol);
		a[lol[0]-'a'][lol[len-1]-'a'] = 1;
		while(scanf("%s",lol) && lol[0] != '0')
		{
			len = strlen(lol);
			a[lol[0]-'a'][lol[len-1]-'a'] = 1;
		}
		dfs(1);
		flag ? printf("YES.\n") : printf("NO.\n");
	}
	return 0;
}


你可能感兴趣的:(HDU 1181 变形课)