hdu 1181 变形课

 
  
#include < stdio.h >
#include
< string .h >
#include
< stdlib.h >
int map[ 27 ][ 27 ],visit[ 26 ];
// const int inf=0x7fffffff;
int main( )
{
char ch[ 30 ];
int i,j,k,len;
memset(visit,
0 , sizeof (visit));
for (i = 0 ;i < 27 ;i ++ )
for (j = 0 ;j < 27 ;j ++ )
map[i][j]
= 0 ;
while (scanf( " %s " ,ch) != EOF)
{

while (ch[ 0 ] != ' 0 ' )
{
len
= strlen(ch);
i
= ch[ 0 ] - ' a ' ;
j
= ch[len - 1 ] - ' a ' ;
map[i][j]
= 1 ;
scanf(
" %s " ,ch);
}
for (k = 0 ;k < 26 ;k ++ )
{
for (i = 0 ;i < 26 ;i ++ )
{
if (i == k || map[i][k] == 0 ) continue ;
for (j = 0 ;j < 26 ;j ++ )
{
if (map[i][j] || map[i][k] && map[k][j]) map[i][j] = 1 ;
}
}
}
if (map[ 1 ][ 12 ]) printf ( " Yes.\n " );
else printf ( " No.\n " );


}




// system("pause");
return 0 ;
}



图的遍历。。。。。。。

另贴上。。我同学lvsi。。。的代码。。方法很多

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操作,此题需注意要剪枝,否则会栈溢出;

转载于:https://www.cnblogs.com/tangcong/archive/2011/04/15/2017007.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值