HDU1181 变形课 DFS

  简单DFS,代码如下:

#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

char word[10000][100];

int hash[10000];

void DFS( char *w, int cnt, int &ans )
{
	if( w[ strlen( w )- 1 ]== 'm' )
	{
		ans= 1;
		return;
	}
	for( int i= 0; i< cnt; ++i )
	{
		if( !hash[i]&& word[i][0] == w[ strlen( w )- 1 ]  )
		{
			hash[i]= 1;
		    DFS( word[i], cnt, ans );
		}
	}
}

int main(  )
{
	int k= 0;
	while( scanf( "%s", word[k++] )!= EOF )
	{
		while( scanf( "%s", word[k++] ), word[k- 1][0]!= '0' ) ;
		int cnt= k,ans= 0;
		k= 0;
		memset( hash, 0, sizeof( hash ) );
		for( int i= 0; i< cnt; ++i )
		{
			if( word[i][0]== 'b'&& !ans )
			{
				DFS( word[i], cnt, ans );
			}
		}
		printf( ans? "Yes.\n": "No.\n" );
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Lyush/p/2135123.html