Firetruck

3 篇文章 0 订阅

题目描述

中心城市消防部门与运输部门合作,维护反映城市街道现状的城市地图。消防员需要能够选择从火警站到火警的路线。
中心城市分为不重叠的消防区。当报告发生火灾时,中央调度员通知火灾发生地区最近的火警站,并列出可能路线。您必须编写一个程序,中央调度员可以使用该程序来生成从地区火警站到火灾的路线。

输入

消防区都用小于 21 的正整数来标识,而且火场始终位于第一个消防区。输入文件包含多个测试用例,代表不同火灾。
• 测试用例的第一行由一个整数组成,该整数是距离火灾最近的火警站。
• 接下来的几行由成对的正整数组成,这些成对的正整数是开放街道相邻的消防区。(例如,如果对 4 7 在一行上,则消防区 4 和消防区 7 之间的街道是开放的。没有其他消防区在 4 和 7 之间。)
• 每个测试用例的最后一行由一对 0 组成。

输出

对于每个测试用例,您的输出必须通过编号来标识用例(“CASE 1:”,"CASE 2:"等)。它必须列出每条路线,并按照字典序从小到大输出。它必须提供从火警站到火灾的总路线。
不同用例的输出必须分开显示。

样例输入

6
1 2
1 3
3 4
3 5
4 6
5 6
2 3
2 4
0 0
4
2 3
3 4
5 1
1 6
7 8
8 9
2 5
5 7
3 1
1 8
4 6
6 9
0 0

样例输出

CASE 1:
1 2 3 4 6
1 2 3 5 6
1 2 4 3 5 6
1 2 4 6
1 3 2 4 6
1 3 4 6
1 3 5 6
There are 7 routes from the firestation to streetcorner 6.
CASE 2:
1 3 2 5 7 8 9 6 4
1 3 4
1 5 2 3 4
1 5 7 8 9 6 4
1 6 4
1 6 9 8 7 5 2 3 4
1 8 7 5 2 3 4
1 8 9 6 4
There are 8 routes from the firestation to streetcorner 4.

代码
//#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstring>
using namespace std;
int mapn[30][30];
int a[30];
bool visit[30];
int n,sum;//sum表记录路径数 

void dfs(int pos,int ans){//pos表示当前的位置,ans用来记录路径的长度 
	if(pos==n){
		sum++;
		cout<<"1";
		for(int i=1;i<ans;i++)
			cout<<" "<<a[i];
		cout<<endl;
	}
	for(int i=2;i<=21;i++){
		if(mapn[pos][i]==1&&!visit[i]){
			visit[i]=1;
			a[ans]=i;
			dfs(i,ans+1);
			visit[i]=0;
		}
	}
}

int main(){
	int time=1;
	while(cin>>n){
		sum=0;
		int x,y;
		memset(a,0,sizeof(a));
		memset(visit,0,sizeof(visit));
		memset(mapn,0,sizeof(mapn));
		while((cin>>x>>y)&&(x!=0)&&(y!=0)){
			mapn[x][y]=mapn[y][x]=1;
		}
		cout<<"CASE "<<time++<<":"<<endl;
		dfs(1,1);
		cout<<"There are "<<sum<<" routes from the firestation to streetcorner "<<n<<"."<<endl;  
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值