凛冬之翼---拯救007困难版

这个题目历时3天都还没写完,主要写是之前对数据结构还没完全了解就开始一股脑的写代码,边写边理解,这是有点浮躁的心态结果这样反而会降低效率。下次记住一定要彻彻底底的理解题目理清数据结构再动笔写代码,一定要觉得不写代码浑身难受再开始写代码!

题目:
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

思路:
1.是一个BFS的思想,但是题目添加的很多测试点特别的恶心,目前为止都还只通过了两个测试点。
2.记住BFS是用到的队列而DFS用到的是递归。

遇到的问题:
1.思路不清晰
2.数据结构不明朗

代码:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;

#define Max 101
#define True 1
#define False 0
#define Infinity 1001

struct crocodile{      //define crocodile's horizontal and vertical coordinates
	int x;
	int y;
}crocodile[Max];

struct dist{         //define dist which contain whether if can be safe and how many jumps is needed from original point
	int value;
	int check;
	int First_Jump;
}dist[Max];

int path[Max];       //used to show the path of a route
int N,D;

void initialize(){
	int x;int y;
	for(int i=1;i<=N;i++){        //set all the data -1
		path[i]=-1;
		dist[i].check=-1;
		dist[i].First_Jump=Infinity;
		dist[i].value=-1;		
	}
	for(int i=1;i<=N;i++){       //get crocodiles
		scanf("%d %d",&x,&y);
		if((7.5*7.5)>(x*x+y*y)) continue;       //if crocodile in the island
		else if((abs(x)>=50)||((abs(y))>=50)) continue; 
		else{
			crocodile[i].x=x;
			crocodile[i].y=y;
		}
//		scanf("%d %d",&crocodile[i].x,&crocodile[i].y);  
	}
}

double Distance(int i,int j){    //calculate distance between two crocodiles
	double x;
	x=sqrt(pow(crocodile[i].x-crocodile[j].x,2)+pow(crocodile[i].y-crocodile[j].y,2)); //sqrt (xi-yj)^2-(yi-yj)^2
	return x;
}

int Escape(int i){             //the check whether 007 can jump from crocodile to bank
    if((crocodile[i].x<=D-50)||(crocodile[i].x>=50-D)||(crocodile[i].y<=D-50)||(crocodile[i].y>=50-D))
	return True;
	else
	return False; 
}

void BFS(){
	int temp;
	queue<int> q;              //ask for a queue
	q.push(0);                //let 0 in the queue
	while(!q.empty()){
		temp=q.front();
		q.pop();               //let data get out
		if(Escape(temp)){
			dist[temp].check=True;                 //set 
//			dist[FirstJump]=FindFirstJump();
		}
		
		for(int i=1;i<=N;i++){
			if((dist[i].value==-1)&&(Distance(temp,i)<=D)) {     //if the crocodile is'nt visited and can be visited
			dist[i].value=dist[temp].value+1;                         //jump and distance add one
			path[i]=temp;
			q.push(i);                                  //let the number in 
			}
		}	
			 
			
	}       
	
	
}

void Path(int number){
	stack<int> s;
	while(path[number]!=-2){
		s.push(number);   //if the point has its father than get its father into stack
		number=path[number];    //change number
	}
	while(!s.empty()){
		int j=s.top();
		s.pop();
		printf("%d %d",crocodile[j].x,crocodile[j].y);       //printf the location of crocodile
		printf("\n");	
	}
}

int SeekFirstJump(int i){
	int FirstPoint;
	int x;
	while(path[i]!=0){
		i=path[i];
	}
	FirstPoint=i;
//	printf("here %d ",FirstPoint);
	x=Distance(0,FirstPoint);
	return x;
}


void Printf(){
	int min=101;
	int number;
	int FirstJump;
	for(int i=0;i<=N;i++){
		if(dist[i].check==1){
			
			if(dist[i].value<=min){           //distance<=min can in 
				if(dist[i].value<min){              //if distance< min change the data directly 
					min=dist[i].value;
					number=i;
					FirstJump=SeekFirstJump(i);   //to find the first jump's distance
					}
				if(dist[i].value==min){
					if(SeekFirstJump(i)<FirstJump){    //if distance==min than compare the firstjump
					min=dist[i].value;
					number=i;
					FirstJump=SeekFirstJump(i);   //to find the first jump's distance
					}		
				}
			}	
		}
	}
	if(min==101){
		printf("0");
	}
	else{
	    printf("%d ",min+1);
	    printf("\n");
	    Path(number);           //printf path
	}		
}



int main(){
	scanf("%d %d",&N,&D); 
	initialize();     //initialize all the data struct
	crocodile[0].x=0;
	crocodile[0].y=0;  //begin in the (0,0)
	dist[0].check=False;dist[0].value=0;dist[0].First_Jump=Infinity;path[0]=-2; // value original point
	BFS();            //the main arithmetic
	Printf();        //printf the result 
	
	
} 

有时间回来看看这个弃坑的代码

猜你喜欢

转载自blog.csdn.net/weixin_39042981/article/details/84309372
今日推荐