uva208

#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深搜即可。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值