#include <iostream>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <cstdio>
using namespace std;
#define INF 0x3f3f3f3f
int n, x, y, c, len;
vector<int> a[25];
int b[25];
bool flag[25];
bool dfs(int x) {
if (x == n) {
c++;
for (int i = 0; i < len; ++i) {
printf("%d ", b[i]);
}
printf("%d\n",n);
return true;
}
vector<int>::iterator it = a[x].begin();
for (; it != a[x].end(); ++it) {
if (!flag[*it]) {
b[len++] = x;
flag[*it] = true;
dfs(*it);
len--;
flag[*it] = false;
}
}
return false;
}
bool bfs() {
memset(flag, false, sizeof(flag));
queue<int> q;
q.push(1);
while (!q.empty()) {
int t = q.front();
q.pop();
vector<int>::iterator it = a[t].begin();
for (; it != a[t].end(); ++it) {
if (!flag[*it]) {
if (*it == n) {
return true;
}
flag[*it] = true;
q.push(*it);
}
}
}
return false;
}
int main() {
int t = 0;
while (cin >> n) {
while (1) {
scanf("%d%d", &x, &y);
if (x == y && x == 0) {
break;
}
a[x].push_back(y);
a[y].push_back(x);
}
for (int i = 1; i <= 20; ++i) {
sort(a[i].begin(), a[i].end());
}
printf("CASE %d:\n",++t);
c = len = 0;
if (bfs()) {
memset(flag, false, sizeof(flag));
flag[1] = true;
dfs(1);
}
printf("There are %d routes from the firestation to streetcorner %d.\n", c, n);
for (int i = 1; i <= 20; ++i) {
a[i].clear();
}
}
return 0;
}
bfs剪枝判断是否可达,dfs深搜即可。