[恢]hdu 1181

2011-12-26 00:07:58

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1181

题意:中文,水。

mark:bfs练习。

代码:

# include <stdio.h>
# include <string.h>


char str[200] ;
int graph[30][30] ;
int visited[30] ;
int q[30] ;


int gao()
{
int i, ch ;
int front = 0, rear = 0 ;
q[rear++] = 1 ;
while (front != rear)
{
ch = q[front++] ;
if (ch == 'm'-'a') return 1 ;
for (i = 0 ; i < 26 ; i++)
{
if (graph[ch][i]==1 && visited[i] == 0)
{
visited[i] = 1 ;
q[rear++] = i ;
}
}
}
return 0 ;
}


void init()
{
memset (graph, 0, sizeof(graph)) ;
memset (visited, 0, sizeof(visited)) ;
}


int main ()
{
while (gets (str))
{
if (strcmp(str, "0") == 0)
{
puts (gao() ? "Yes." : "No.") ;
init() ;
}
else
{
graph[str[0]-'a'][str[strlen(str)-1]-'a'] = 1 ;
}
}
return 0 ;
}



原文地址:https://www.cnblogs.com/lzsz1212/p/2315353.html