概述
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
基本思路
-
根据所给的坐标,初始化一个任意两点间距离的二维数组,同时储存可以上岸的点
-
使用Folyd递推更新任意两点间的距离
-
对每一个中心圆盘可到达的点,对其分别遍历与可上岸点间的距离
需要注意的情况:距离相同时,选离中心圆盘近的点;最后一个测试点,一jio可以直接跨上岸的离谱长腿欧巴
AC代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MaxVertexNum 101
#define Position int
#define INFINITY 65535
typedef int Vertex;
//储存坐标
typedef struct
{
Position X;
Position Y;
}Node[MaxVertexNum];
typedef struct GNode* MGraph;
struct GNode
{
int Nv;
Node G;
};
//任意两点间的距离
int Dist[MaxVertexNum][MaxVertexNum];
//任意两点间的中转点
int Path[MaxVertexNum][MaxVertexNum];
//储存可以上岸的点
int F[MaxVertexNum];
int F_len=-1;
/*判断是否可以跳到下一个顶点*/
int Jump(Vertex V,Vertex W,int D,MGraph Graph){
int Flag=0;
/*两点间的距离是否小于D*/
if (sqrt(((Graph->G[V].X-Graph->G[W].X)*(Graph->G[V].X-Graph->G[W].X))+((Graph->G[V].Y-Graph->G[W].Y)*(Graph->G[V].Y-Graph->G[W].Y)))<=D)
Flag=1;
return Flag;
}
/*判断是否可以上岸*/
int Is_Safe(Vertex V,int D,MGraph Graph){
int Flag=0;
if (((abs(Graph->G[V].X)+D)>=50)||((abs(Graph->G[V].Y)+D)>=50))
Flag=1;
return Flag;
}
void Folyd(MGraph Graph,int D){
Vertex V,W;
int i,j,k;
//初始化
for ( V = 0; V < Graph->Nv; V++)
{
for ( W = 0; W < Graph->Nv; W++)
{
if (V!=W)
{
if (Jump(V,W,D,Graph))
Dist[V][W]=1;//如果两点间可跳跃,更新距离为1
else
Dist[V][W]=INFINITY;//否则,距离无穷大
}
}
//判断该点是否可以上岸
if (Is_Safe(V,D,Graph))
F[++F_len]=V;
}
//Folyd,递推任意点间的距离
for ( k = 0; k < Graph->Nv; k++)
for ( i = 0; i < Graph->Nv; i++)
for ( j = 0; j < Graph->Nv; j++)
if (Dist[i][k]+Dist[k][j]<Dist[i][j])
{
Dist[i][j]=Dist[i][k]+Dist[k][j];
Path[i][j]=k;
}
}
/*判断是否可以由中心跳到该结点*/
int First_Jump(Vertex V,int D,MGraph Graph){
int Flag=0;
/*(0,0)为圆心,(D+中心半径)为半径的圆*/
if (((Graph->G[V].X)*(Graph->G[V].X)+(Graph->G[V].Y*Graph->G[V].Y))<=((7.5+D)*(7.5+D)))
Flag=1;
return Flag;
}
//递归输出路径
void Display(Vertex V,Vertex W,MGraph Graph){
if (Path[V][W]!=-1)
{
Display(V,Path[V][W],Graph);
printf("%d %dn",Graph->G[Path[V][W]].X,Graph->G[Path[V][W]].Y);
Display(Path[V][W],W,Graph);
}else
return;
}
void Svae007(MGraph Graph,int D){
Vertex V,W;
Vertex MinS,MinF;//最小距离的起点终点
int MinDis=INFINITY;
float Dis=INFINITY;//与中心圆盘的最小距离
for (V=0; V<Graph->Nv; V++){
if ((First_Jump(V,D,Graph)==1))
{
//寻找每一个起点到岸的最小距离
for ( W = 0; W <= F_len ; W++)
{
//找到更小的
if (Dist[V][F[W]]<MinDis)
{
MinS=V;
MinF=F[W];
MinDis=Dist[V][F[W]];
Dis=sqrt((Graph->G[V].X*Graph->G[V].X)+(Graph->G[V].Y)*Graph->G[V].Y);
}
//距离相等,比较与中心圆盘的距离
if (Dist[V][F[W]]==MinDis){
float Temp=sqrt((Graph->G[V].X*Graph->G[V].X)+(Graph->G[V].Y)*Graph->G[V].Y);
if (Temp<Dis)
{
MinS=V;
MinF=F[W];
MinDis=Dist[V][F[W]];
Dis=Temp;
}
}
}
}
}
if (MinDis!=INFINITY)//可上岸
{
printf("%dn",MinDis+2);
printf("%d %dn",Graph->G[MinS].X,Graph->G[MinS].Y);
Display(MinS,MinF,Graph);
printf("%d %d",Graph->G[MinF].X,Graph->G[MinF].Y);
}else
printf("0");
}
int main()
{
int N,D;
MGraph Graph;
Graph=(MGraph)malloc(sizeof(struct GNode));
memset(Path,-1,sizeof(Path));
scanf("%d %d",&Graph->Nv,&D);
for (int i = 0; i < Graph->Nv; i++)
scanf("%d %d",&Graph->G[i].X,&Graph->G[i].Y);
if ((7.5+D)>=50)//一jio可以上岸的变态情况
{
printf("1");
return 0;
}
Folyd(Graph,D);
Svae007(Graph,D);
return 0;
}
最后
以上就是疯狂酸奶为你收集整理的Saving James Bond - Hard Version的全部内容,希望文章能够帮你解决Saving James Bond - Hard Version所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复