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操作,此题需注意要剪枝,否则会栈溢出;
posted on 2011-04-15 12:55 more think, more gains 阅读(255) 评论(0) 编辑 收藏 举报