HDU 1181 变形课(BFS)
裸BFS,不复杂。
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 int p[27][27],o[27],key[27]; 5 int main() 6 { 7 int i,j,len,end,start,a,z; 8 char str[1000]; 9 while(scanf("%s",str)!=EOF) 10 { 11 z = 0; 12 memset(p,0,sizeof(p)); 13 memset(o,0,sizeof(o)); 14 len = strlen(str); 15 p[str[0]-'a'+1][str[len-1]-'a'+1] = 1; 16 for(;;) 17 { 18 scanf("%s",str); 19 if(str[0] == '0') 20 break; 21 len = strlen(str); 22 p[str[0]-'a'+1][str[len-1]-'a'+1] = 1; 23 } 24 start = end = 1; 25 key[1] = 2; 26 while(start <= end) 27 { 28 j = 1; 29 for(i = start;i <= end;i ++) 30 { 31 if(o[key[i]] == 0) 32 { 33 for(a = 1;a <= 26;a ++) 34 { 35 if(p[key[i]][a] && !o[a]) 36 { 37 key[end+j] = a; 38 j ++; 39 } 40 } 41 o[key[i]] = 1; 42 } 43 } 44 start = end + 1; 45 end = end + j - 1; 46 } 47 if(o[13]) z = 1; 48 if(z) 49 printf("Yes.\n"); 50 else 51 printf("No.\n"); 52 } 53 return 0; 54 }