LeeBlog

导航

hdu 1181 变形课

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int len,map[26][ 26 ],des[26],a,b,f = 0,m;
char ch[1000];
void DFS( int n )
{
     if( f )
         return ;              
     if( n == 'm' - 'a' )
     {
         f = 1;
         return;
     }
     for( int i = 0; i < 26 ; ++i )
     {
          if( n != i && map[n][i] == 1 )
          {
              if( des[ i ] == 1 )
                  return;
              des[ i ] = 1;
              DFS( i );
              des[ i ] = 0;
          }
          if( f )
              return ;
      }
      
 }
int main( )
{
    while( scanf( "%s",ch ) && ch[ 0 ] != '0' )
    {
           memset( des,0,sizeof( des ) );
           memset( map,0,sizeof( map ) );
           f = 0;
           len = strlen( ch );
           a = ch[ 0 ] - 'a';
           b = ch[ len - 1 ] - 'a';
           map[ a ][ b ] = 1;
           while( scanf( "%s",ch ) && ch[ 0 ] != '0' )
           {
                  len = strlen( ch );
                  a = ch[ 0 ] - 'a';
                  b = ch[ len - 1 ] - 'a';
                  map[ a ][ b ] = 1;
           }
           DFS( 1 );
           f == 1 ? printf( "Yes.\n" ) : printf( "No.\n" );
           }
    return 0;
}
这题考察遍历图的DFS操作,此题需注意要剪枝,否则会栈溢出;

posted on 2011-03-01 22:40  LeeBlog  阅读(205)  评论(0编辑  收藏  举报