07-图5 Saving James Bond - Hard Version

这个题是hard版本的007逃亡,具体要求是,不仅要判断能不能让007逃出孤岛,还要输出最短的逃出路径。在简单版本中,利用深度优先或者广度优先都能进行判断,但若要输出最短的逃出路径,深度优先显然不合适了,因为它是一条道走到黑的…


先来分析节点和路径,节点很明显,每一个鳄鱼,路径还是和简单版本的一样,能跳过去的就是一条路径。这里其实可以看出,每一条路径只有“存在”和“不存在”两种状态,整张图是一张无权图。在判断能否跳出去的时候,利用广度优先搜素,一圈一圈的往外搜索,直到某一次可以跳到岸上,或者直到最远圈都跳不出去。


这样看来,其实这个hard版本的007大逃亡和六度空间理论那题是一样的,只是在何时结束的有所区分。输出最短路径的话,在记录的table里面加pre这一栏,然后递归输出就好。


对了,注意鳄鱼在岛上和鳄鱼在岸上这两种情况。在这两种情况下,貌似都是默认不会跳到他们头上的~~(毕竟不是马里奥…踩不死)


#include 
#include 
#include 
#define R 7.5

typedef struct _node{
	int x;
	int y;
}Node;

typedef struct _list{
	int location;
	struct _list *next;
}List;

typedef struct _table{
	int know; //记录是否被访问过了,没有写0,有了写1,能到岸上写2 
	int dv;  //记录总路径长短 
	int pre; //记录上一个节点 
}Table;
 
int  Number;
int  Stepsize;
Node *Croco;
Table *Sheet;

int   find_shore(int x,int y,int range);
void  add_que(List *Beginer,int loc);
int   dele_que(List *Beginer);
void  Jump();
void  print_table();
int   find_root(int i);
int   find_termination();
void  print_croco(int loc);

int main(){
	scanf("%d %d",&Number,&Stepsize);
	Croco=(Node*)malloc((Number+1)*sizeof(Node));
	Sheet=(Table*)malloc((Number+1)*sizeof(Table));
	int i,j,k;
	for(k=0;knext=NULL;
	
	
	int record=0;
	int i=1;
	int reach=0;
	int k;
	add_que(Beginer,0); //把原点在Croco中的下标压进队列 
	record=dele_que(Beginer);  //原点出队列(出来的是在Croco中的下标) 
	reach=find_shore(Croco[record].x,Croco[record].y,Stepsize+R);  //看看能不能到岸上 
	if(reach){  //能,Sheet中这点的Know写2 
		Sheet[record].know=2;
	}else{    //不行就写1 
		Sheet[record].know=1;
	}
	
	for(k=0;knext){
		
		record=dele_que(Beginer);  //原点出队列(出来的是在Croco中的下标) 
		reach=find_shore(Croco[record].x,Croco[record].y,Stepsize);  //看看能不能到岸上 
		if(reach){  //能,Sheet中这点的Know写2 
			Sheet[record].know=2;
		}
		
		
		for(k=0;k pow(7.5,2) && (abs(Croco[k].x)<50 && abs(Croco[k].y<50))){
				if(Sheet[k].know==0 && pow(Croco[k].x-Croco[record].x,2) + pow(Croco[k].y-Croco[record].y,2)<=pow(Stepsize,2)){
					add_que(Beginer,k);  //把这个点的下标写进去, 
					Sheet[k].dv=Sheet[record].dv+1;  //距离为之前节点距离+1 
					Sheet[k].pre=record;  //记录之前节点 
					Sheet[k].know=1;
				}
			}
		}
		
	}		
} 

void print_croco(int loc){
	if(Sheet[loc].pre==0){
		printf("%d %d\n",Croco[loc].x,Croco[loc].y);
	}
	else{
		print_croco(Sheet[loc].pre);
		printf("%d %d\n",Croco[loc].x,Croco[loc].y);
	}
}

int   find_root(int i){
	int loc;
	while(Sheet[i].pre != 0){
		 i=Sheet[i].pre;
	}
	loc=i;
	return loc;
}

int   find_termination(){
	int i;
	int Dis=200;
	int First=1000;
	int loction=-1;
	for(i=0;inext){
		p=p->next;
	}
	List *q=(List*)malloc(sizeof(List));
	q->next=NULL;
	q->location=loc;
	p->next=q;
}

int dele_que(List *Beginer){
	List *record;
	record=Beginer->next;
	if(record){
		Beginer->next=record->next;
	}
	int result=record->location;
	free(record);
	return result;
}

int find_shore(int x,int y,int range){
	
	int flag=0;
	if(x>=0){	x=x+range;	}
	else{		x=x-range;	}
	
	if(y>=0){	y=y+range;	}
	else{		y=y-range;	}
	
	if(abs(x)>=50 || abs(y)>=50){
		flag=1;
	}
	return flag;
}

void print_table(){
	int i=0;
	for(i=0;i



07-图5 Saving James Bond - Hard Version   (30分)

This time let us consider the situation in the movie "Live and Let Die" in which James Bond, the world's most famous spy, was captured by a group of drug dealers. He was sent to a small piece of land at the center of a lake filled with crocodiles. There he performed the most daring action to escape -- he jumped onto the head of the nearest crocodile! Before the animal realized what was happening, James jumped again onto the next big head... Finally he reached the bank before the last crocodile could bite him (actually the stunt man was caught by the big mouth and barely escaped with his extra thick boot).

Assume that the lake is a 100 by 100 square one. Assume that the center of the lake is at (0,0) and the northeast corner at (50,50). The central island is a disk centered at (0,0) with the diameter of 15. A number of crocodiles are in the lake at various positions. Given the coordinates of each crocodile and the distance that James could jump, you must tell him a shortest path to reach one of the banks. The length of a path is the number of jumps that James has to make.

Input Specification:

Each input file contains one test case. Each case starts with a line containing two positive integers N (100), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the (x,y) location of a crocodile. Note that no two crocodiles are staying at the same position.

Output Specification:

For each test case, if James can escape, output in one line the minimum number of jumps he must make. Then starting from the next line, output the position (x,y) of each crocodile on the path, each pair in one line, from the island to the bank. If it is impossible for James to escape that way, simply give him 0 as the number of jumps. If there are many shortest paths, just output the one with the minimum first jump, which is guaranteed to be unique.

Sample Input 1:

17 15
10 -21
10 21
-40 10
30 -50
20 40
35 10
0 -10
-25 22
40 -40
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10

Sample Output 1:

4
0 11
10 21
10 35

Sample Input 2:

4 13
-12 12
12 12
-12 -12
12 -12

Sample Output 2:

0

你可能感兴趣的:(07-图5 Saving James Bond - Hard Version)