Uva208救火车(回溯)(T)

版权声明:本文为博主原创文章,转载请声明原创网址。 https://blog.csdn.net/lagoon_lala/article/details/82353600

题目:

The Center City fire department collaborates with the transportation department to maintain maps of the city which reflects the current status of the city streets. On any given day(任何一天), several streets are closed for repairs or construction. Firefighters need to be able to select routes from the fire stations to fires that do not use closed streets.

Central(中心) City is divided into non-overlapping(不重叠) fire districts, each containing a single fire station. When a fire is reported, a central dispatcher(调度员) alerts the fire station of the district where the fire is located and gives a list of possible routes from the fire station to the fire. You must write a program that the central dispatcher can use to generate routes from the district fire stations to the fires.

Input

The city has a separate map for each fire district. Street corners of each map are identified by positive integers less than 21, with the fire station always on corner #1. The input file contains several test cases representing different fires in different districts.

• The first line of a test case consists of a single integer which is the number of the street corner closest to the fire.

• The next several lines consist of pairs of positive integers separated by blanks which are the adjacent(相邻) street corners of open streets. (For example, if the pair 4 7 is on a line in the file, then the street between street corners 4 and 7 is open. There are no other street corners between 4 and 7 on that section(段) of the street.)

• The final line of each test case consists of a pair of 0’s.

Output

For each test case, your output must identify the case by number (‘CASE 1:’, ‘CASE 2:’, etc). It must list each route on a separate line, with the street corners written in the order in which they appear on the route. And it must give the total number routes from fire station to the fire. Include only routes which do not pass through any street corner more than once. (For obvious reasons, the fire department doesn’t want its trucks driving around in circles.)

Output from separate cases must appear on separate lines.

思路:

双向邻接图用矩阵存(为方便不使用0),全局变量记路数量,循环开始清零

消防车默认为1,输入火点,构造图(街角最多21),以0,0为读入图结束

1遍历图到火点,回溯需要用栈倒)对街角判重,(不同路用不同vis),能到达火点则路数+1

如何存储分叉路(判断存在第几个方案),将地图改造成无死路,最后打印时根据记录的该店分叉数和打印次数循环

用链表,传入fa(尾到头)?

出现分叉路把前面的都重新输出一遍?

传入不同的路径数组,如果能通,直接输出

Debug:

用scanf作为while判断条件时,记得判断返回参数个数,否则可能因为负数为真等出现空循环

遍历,循环尝试值时,存入位置固定,不能随意改动

求所有解,找到的判断不能放在遍历循环内,否则return会打断

所有样例答案都能对上,但是T了,据说裸搜会超时,学成后再战

代码:

/*
6
1 2
1 3
3 4
3 5
4 6
5 6
2 3
2 4
0 0

*/ 
#include<stdio.h>
#include<string.h>
//#define LOCAL 

int corn[23][23];//corner
int vis[23];
int fire,maxcor; 
int cases=1,routs;//案例数与道路方案总数 
void draw(){
	printf("  ");
	for(int i=1;i<=maxcor;i++){
		printf("%d ",i);
	}
	printf("\n");
	for(int i=1;i<=maxcor;i++){
		printf("%d:",i);
		for(int j=1;j<=maxcor;j++){
			if(corn[i][j])printf("%d ",corn[i][j]);
			else printf("  ");
		}
		printf("\n");
	}
}
void print_path(int p[],int cnt){
	for(int i=0;i<cnt;i++){
		printf("%d",p[i]);
		if(i!=cnt-1)printf(" ");
	}
	printf("\n");
}
void search(int per,int p[],int cnt){
	vis[per]=1;
	int path[23];
	memcpy(path,p,sizeof(path));
	if(per==fire){
		print_path(path,cnt);
		routs++;
		return ;
	}
	
	for(int i=1;i<=maxcor;i++){
		if(corn[per][i]&&!vis[i]){//访问与per相邻的结点i
//	 		printf("search %d\n",i);
	 		path[cnt]=i;
	 		
			search(i,path,cnt+1);
		 	vis[i]=0;
		}
	}
}
int main(){
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
	while(scanf("%d",&fire)==1){
		memset(corn,0,sizeof(corn));
		memset(vis,0,sizeof(vis));
		int pre,sec;//输入两个街角 
		maxcor=0;//最大街角数 
		routs=0;
		printf("CASE %d:\n",cases++) ;
		while(scanf("%d%d",&pre,&sec)==2&&pre&&sec){
			corn[pre][sec]=1;
			corn[sec][pre]=1;
			if((pre>sec?pre:sec)>maxcor)maxcor=pre>sec?pre:sec; 
		}
//		draw();
		int path[23] ;
		path[0]=1;
		search(1,path,1);
		printf("There are %d routes from the firestation to streetcorner %d.\n",routs,fire);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lagoon_lala/article/details/82353600