#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <queue>
#define N 676 /* all situation for city name */
struct node {
int cityNumber;
struct node *neighbor;
int path;
}; /* ---------- end of struct node ---------- */
typedef struct node Node;
/*
* === FUNCTION ======================================================================
* Name: printAns
* Description:
* =====================================================================================
*/
void
printAns ( Node m[], int start, int end)
{
int temp1 = 0 ,temp2 = 0;
if ( start != end ) {
printAns( m, start, m[end].path);
}
else {
return ;
}
temp1 = m[ m[end].path ].cityNumber / 26;
temp2 = m[ m[end].path ].cityNumber % 26;
printf("%c%c ", temp1+'A', temp2+'A');
temp1 = m[end].cityNumber / 26;
temp2 = m[end].cityNumber % 26;
printf("%c%c\n", temp1+'A', temp2+'A');
return ;
} /* ----- end of function printAns ----- */
/*
* === FUNCTION ======================================================================
* Name: bfs
* Description:
* =====================================================================================
*/
void
bfs (bool v[], Node m[], int start, int end )
{
int currentCityNum, pathRecord;
Node *currentCity;
std::queue<int> que;
que.push(start);
v[start] = true;
m[start].path = start;
if ( start == end ) {
int temp1 ,temp2;
temp1 = m[ start ].cityNumber / 26;
temp2 = m[ start ].cityNumber % 26;
printf("%c%c ", temp1+'A', temp2+'A');
temp1 = m[end].cityNumber / 26;
temp2 = m[end].cityNumber % 26;
printf("%c%c\n", temp1+'A', temp2+'A');
return ;
}
while ( !que.empty()) {
pathRecord = que.front();
que.pop();
currentCity = &m[pathRecord];
while ( NULL != currentCity ) {
currentCityNum = currentCity->cityNumber;
if ( !v[currentCityNum] ) {
currentCity->path = pathRecord;
m[currentCityNum].path = pathRecord;
v[currentCityNum] = true;
if ( end == currentCityNum ) {
printAns( m, start, end);
return ;
}
else {
que.push(currentCityNum);
}
}
currentCity = currentCity->neighbor;
}
}
printf ( "No route\n" );
return ;
} /* ----- end of function bfs ----- */
/*
* === FUNCTION ======================================================================
* Name: addEdge
* Description:
* =====================================================================================
*/
void
addEdge (Node map[], int city1, int city2 )
{
Node *temp;
/* city1 -> city2 */
temp = (Node*)malloc(sizeof(Node));
temp->cityNumber = city2;
temp->neighbor = map[city1].neighbor;
map[city1].neighbor = temp;
map[city1].cityNumber = city1;
/* city2 -> city1 */
temp = (Node*)malloc(sizeof(Node));
temp->cityNumber = city1;
temp->neighbor = map[city2].neighbor;
map[city2].neighbor = temp;
map[city2].cityNumber = city2;
return ;
} /* ----- end of function addEdge ----- */
/*
* === FUNCTION ======================================================================
* Name: main
* Description:
* =====================================================================================
*/
int
main ( int argc, char *argv[] )
{
char name1[3], name2[3];
int city1 = 0, city2 = 0, datas = 0;
bool visit[N];
Node map[N];
while ( scanf ( "%d", &datas )!= EOF ) {
memset(visit, false, sizeof(bool)*N);
for ( int i = 0; i<N; i++ ) {
map[i].neighbor = NULL;
}
for ( int i = 0; i<datas; i++ ) {
scanf ( "%s%s", &name1, &name2 );
city1 = (name1[0]-'A')*26+(name1[1]-'A');
city2 = (name2[0]-'A')*26+(name2[1]-'A');
addEdge(map, city1, city2);
}
scanf ( "%s%s", &name1, &name2 );
city1 = (name1[0]-'A')*26+(name1[1]-'A');
city2 = (name2[0]-'A')*26+(name2[1]-'A');
printf ( "\n" );
bfs(visit, map, city1, city2);
printf ( "\n" );
}
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */