7-11 Saving James Bond - Hard Version

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

具体代码实现为:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib> 
using namespace std;

#define TRUE 1
#define FALSE 0
#define MaxVertexNum 105    /* 最大顶点数设为100 */
#define ElementType int
typedef int Vertex;         /* 用顶点下标表示顶点,为整型 */

struct coordinate{  //鳄鱼坐标 
	int x;
	int y;
}crocodile[105];

typedef struct SNode *Stack;  //堆栈数据结构 
struct SNode{
	ElementType *data;
	int top;
	int MaxSize; 
};

int N,D;

void Save007();

int main()
{
	int i;
	
	scanf("%d %d",&N,&D);  //输入鳄鱼数量及007可以跳跃的最大距离
	for(i=0; i<N; i++){
		scanf("%d %d",&crocodile[i].x,&crocodile[i].y);
	}
	if(D >= 42.5){  /* 可以直接从孤岛跳到岸上,直接输出1 */
		printf("%d\n",1);
	}else{
		Save007();
	}
	
	return 0;
}

int IsSafe(int v)
{
	int X_distant,Y_distant;
	
	X_distant = abs(crocodile[v].x)-50;
	Y_distant = abs(crocodile[v].y)-50;
	
	return (abs(X_distant) <= D || abs(Y_distant) <= D);
}

int Jump(int v,int w)
{
	/*两个鳄鱼坐标之间的距离是否小于等于Bond可以跳跃的最大距离D*/
	return (sqrt(pow(crocodile[v].x-crocodile[w].x,2)+pow(crocodile[v].y-crocodile[w].y,2)) <= D);	
}

int FirstJump(int v)
{
	int d;
	
	d = sqrt(pow(crocodile[v].x,2)+pow(crocodile[v].y,2));
	if(d <= D+7.5){
		return d;
	}else{
		return 0;
	}
}
//建立空栈 
Stack CreateStack(int MaxSize)
{
    Stack S = (Stack)malloc(sizeof(struct SNode));
    S->data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
    S->top = -1;
    S->MaxSize = MaxSize;
    return S;
}
 //判栈空 
 bool IsEmpty ( Stack S )
{ /* 判断堆栈S是否为空,若是返回true;否则返回false */
    return ( S->top == -1 );
}
//入栈
void Push(Stack PtrS,ElementType X)
{
	if(PtrS->top==PtrS->MaxSize-1)
	    printf("堆栈满\n"); 
	else
	    PtrS->data[++(PtrS->top)]=X;
 } 
//出栈
#define Error -1
ElementType Pop(Stack PtrS)
{
	if(PtrS->top==-1){
		printf("堆栈空\n");
		return Error; 
	}else{
		return (PtrS->data[(PtrS->top)--]);
	}
}

bool cmp(int a,int b)
{
	return FirstJump(a) < FirstJump(b);
}

void Save007()
{
    Vertex V,W;
	Vertex a[MaxVertexNum];  //结点队列
    int head = 0,tail = 0;   //队列头尾指针
    int curlast,last;  //curlast为当前BFS访问层所访问的最后一个结点,last为上一BFS访问层所访问的最后一个结点
	int i,b[MaxVertexNum];
	int step;  //记录最少要跳跃的次数 
	int path[MaxVertexNum] = {-1};//存储跳跃的路径 
	int visited[105] = {FALSE};  //标志某一个鳄鱼坐标是否被访问过,=FALSE未被访问过 
	
	for(i=0; i<N; i++){
		b[i] = i;
	}
	sort(b,b+N,cmp);  /* 按照第一步的距离升序排序 */
	for(i=0; i<N; i++){
		if(FirstJump(b[i])){  //能跳上去 
			a[tail++] = b[i];  //入队 
			visited[b[i]] = TRUE;//标记为已访问 
			//curlast = b[i];
			last = b[i];
		}
	}
    step = 1; 
     
    while ( head < tail ) {   //队列不为空时
        V = a[head++];  /* 弹出V */
        if(IsSafe(V)){
        	step++;  //安全时跳到岸上要多一步 
        	printf("%d\n",step);
        	Stack s = CreateStack(N);
        	while(step > 1){/*把逃生路径所经过的结点全部压到堆栈中(注意这个是反向的)*/ 
        		Push(s,V);
        		V = path[V];
        		step--;
			}
			while(!IsEmpty(s)){/*输出堆栈中的结点(反向变为正向)*/ 
				V = Pop(s);
				printf("%d %d\n",crocodile[V].x,crocodile[V].y);
			}
			return;
		}
        for(W=0; W<N; W++){
  			if ( !visited[W] && Jump(V,W) ) {/*对于V的每一个没有被访问过且可以跳跃到的结点*/ 
  				visited[W] = TRUE;
                path[W] = V;  /*将V记录在源点(孤岛)(0,0)到W的路径上*/
                a[tail++] = W; /* W入队列 */
                curlast = W;  // 更新curlast
            }
		}
		if(V == last){/*即将进入BFS的下一层*/
        	step++;
        	last = curlast;//更新curlast
		}
    }
    if(head == tail){   //队列等于空,说明007没跳出去 
    	printf("%d\n",0); 
	} 
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值