七、图(中):Saving James Bond - Hard Version

2023-12-30 14:58
文章标签 version hard bond james saving

本文主要是介绍七、图(中):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<stdio.h>
#include<malloc.h>
#include<math.h>
#define MaxVertexNum 100
#define INFINITYMAX 65535
#define Radius 7.5
typedef int Vertex;
typedef double WeightType;
int path[MaxVertexNum][MaxVertexNum];
int MaxDist;
WeightType D[MaxVertexNum][MaxVertexNum];typedef struct ENode *PtrToENode;
struct ENode{Vertex V1, V2;WeightType Weight;
};
typedef PtrToENode Edge;typedef struct GNode *PtrToGNode;
struct GNode{int Nv;int Ne;WeightType G[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGNode MGraph;struct Location{int x;int y;
}Loc[MaxVertexNum];typedef struct MinPath *PtrToMinPath;
struct MinPath{/* 存储最短路径 */Vertex FirstNode;Vertex LastNode;WeightType Dist;
};MGraph CreateGraph( int VertexNum )
{Vertex V, W;MGraph Graph;Graph = (MGraph)malloc(sizeof(struct GNode));Graph->Nv = VertexNum;Graph->Ne = 0;for( V=0; V!=Graph->Nv; ++V )for( W=0; W!= Graph->Nv; ++W )Graph->G[V][W] = INFINITYMAX;return Graph;
}void InsertEdge( MGraph Graph, Edge E )
{Graph->G[E->V1][E->V2] = E->Weight;Graph->G[E->V2][E->V1] = E->Weight;
}double CalDist( struct Location Loc1, struct Location Loc2 )
{return sqrt( pow(Loc1.x-Loc2.x,2) + pow(Loc1.y-Loc2.y,2) );	
}MGraph BuildGraph()
{MGraph Graph;Edge E;int Nv, i, j=0, x, y;scanf("%d %d",&Nv,&MaxDist);for( i=0; i!=Nv; ++i ){scanf("\n%d %d",&x,&y);if ( sqrt(pow(x,2)+pow(y,2))>=Radius && x<=50 && x>=-50 && y<=50 && y>=-50 ){Loc[j].x = x;Loc[j].y = y;++j;}}Graph = CreateGraph( j-1 );E = (Edge)malloc(sizeof(struct ENode));for( i=0; i!=Graph->Nv; ++i )for ( j=i; j!=Graph->Nv; ++j ){if( i!=j ){E->V1 = i;E->V2 = j;if(CalDist(Loc[i], Loc[j]) <= MaxDist)E->Weight = CalDist( Loc[i], Loc[j] );else E->Weight = INFINITYMAX;InsertEdge(Graph, E); }}	return Graph;
}void Floyd( MGraph Graph, WeightType D[][MaxVertexNum] )
{Vertex i, j ,k;for( i=0; i!=Graph->Nv; ++i )for ( j = 0; j!=Graph->Nv; ++j ){D[i][j] = Graph->G[i][j];path[i][j] = -1;}for( k=0; k!=Graph->Nv; ++k )for( i=0; i!=Graph->Nv; ++i )for( j=0; j!=Graph->Nv; ++j )if( D[i][k] + D[k][j] < D[i][j] ){D[i][j] = D[i][k] + D[k][j];path[i][j] = k;}
}int FirstJump(struct Location Loc)
{if(pow(Radius+MaxDist,2)>=pow(Loc.x,2)+pow(Loc.y,2))return 1;else return 0;
}WeightType LastJump(Vertex i)
{int min = abs(50-Loc[i].x);if(abs(Loc[i].y+50)<min)min = abs(Loc[i].y+50);if(abs(50-Loc[i].y)<min)min = abs(50-Loc[i].y);if(abs(50+Loc[i].y)<min)min = abs(50+Loc[i].y);return min;
}PtrToMinPath Compare(Vertex i, Vertex j, PtrToMinPath P)
{WeightType LastDist = LastJump(i);WeightType DD = D[i][j]+sqrt(pow(Loc[j].x,2)+pow(Loc[j].y,2)) + LastDist;if(i==j)DD = sqrt(pow(Loc[j].x,2)+pow(Loc[j].y,2)) + LastDist;if ( (DD < P->Dist) || ( DD == P->Dist && pow(Loc[j].x,2)+pow(Loc[j].y,2) < pow(Loc[P->FirstNode].x,2)+pow(Loc[P->FirstNode].y,2) ) ){/* 如果从i到j的距离小于最小距离,或者等于最小距离同时第一跳的距离最短 */P->FirstNode = j;P->LastNode = i;P->Dist = DD;}return P;
}struct Location Queue[MaxVertexNum];
Vertex last = -1;void ListNode(Vertex i, Vertex j)
{while(1){Queue[++last] = Loc[j];if(i==j)break;j = path[i][j];if(j==-1){Queue[++last] = Loc[i];break;}}printf("%d\n",last+2);while(last!=-1){printf("%d %d\n",Queue[last].x,Queue[last].y);--last;}
}int main()
{Vertex i, j;PtrToMinPath P;P = (PtrToMinPath)malloc(sizeof(struct MinPath));P->Dist = INFINITYMAX;MGraph Graph = BuildGraph();if(MaxDist+Radius>=50){printf("1\n");return 0;}Floyd(Graph, D);for( i=0; i!=Graph->Nv; ++i ){/* 扫描所有结点,找到能跳到岸上的结点 */if(Loc[i].x>=50-MaxDist || Loc[i].x<=MaxDist-50 || Loc[i].y>=50-MaxDist || Loc[i].y<=MaxDist-50){for(j=0; j!=Graph->Nv; ++j)if(FirstJump(Loc[j]))/* 扫描所有结点,找到第一步能跳到的结点,并判断跳到岸上的距离是不是最小 */P = Compare(i,j,P);}}if(P->Dist == INFINITYMAX)printf("0\n");else ListNode(P->FirstNode,P->LastNode);return 0;
}

出现的问题和解决方法

题目看错了,人家问的是跳跃次数最少的路径,我写成了跳跃距离最短的路径,神奇的是只有一个测试点“最大N,sample1的复杂版,可选路径8条,后面要更新前面的最短路”通不过。改了一下午,今天天气特别热,实在是不行了,懒得改了,就按最小距离得了。
另外,如果鳄鱼在小岛上,詹姆斯邦德不是直接就被吃了吗,所以这种情况按理说应该直接输出0就对了啊。有问题。

这篇关于七、图(中):Saving James Bond - Hard Version的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/553207

相关文章

提示:Decompiled.class file,bytecode version如何解决

《提示:Decompiled.classfile,bytecodeversion如何解决》在处理Decompiled.classfile和bytecodeversion问题时,通过修改Maven配... 目录问题原因总结问题1、提示:Decompiled .class file,China编程 bytecode

Maven创建项目中的groupId, artifactId, 和 version的意思

文章目录 groupIdartifactIdversionname groupId 定义:groupId 是 Maven 项目坐标的第一个部分,它通常表示项目的组织或公司的域名反转写法。例如,如果你为公司 example.com 开发软件,groupId 可能是 com.example。作用:groupId 被用来组织和分组相关的 Maven artifacts,这样可以避免

Jenkins 通过 Version Number Plugin 自动生成和管理构建的版本号

步骤 1:安装 Version Number Plugin 登录 Jenkins 的管理界面。进入 “Manage Jenkins” -> “Manage Plugins”。在 “Available” 选项卡中搜索 “Version Number Plugin”。选中并安装插件,完成后可能需要重启 Jenkins。 步骤 2:配置版本号生成 打开项目配置页面。在下方找到 “Build Env

Learn ComputeShader 09 Night version lenses

这次将要制作一个类似夜视仪的效果 第一步就是要降低图像的分辨率, 这只需要将id.xy除上一个数字然后再乘上这个数字 可以根据下图理解,很明显通过这个操作在多个像素显示了相同的颜色,并且很多像素颜色被丢失了,自然就会有降低分辨率的效果 效果: 但是这样图像太锐利了,我们加入噪声去解决这个问题 [numthreads(8, 8, 1)]void CSMain(uint3 id

HDU 1097 A hard puzzle(规律)

题目: http://acm.hdu.edu.cn/showproblem.php?pid=1097 题意: 求a的b次方的最后一位。 题解: 直接从例子入手, 第一组数据 7 66,结果如下(只要最后一位所以模10) 7 9 3 1 7 9··· 循环节为4,即结果在4个数值内循环出现。 第二组数据 6 800,结果如下 6 6 6 6··· 循环节为1 ···

Unsupported major.minor version 52.0 错误解决方法

自己前些天的项目突然出现这个问题,经过仔细排查,发现有两个原因都会导致这个问题。 第一个就是POM文件中的dependency重复,如果使用的是maven导入,重复写入dependency就会出现该错误。 第二个是版本不匹配,即所引用的jar包太新,并不匹配你的jdk,因为我们正常用的都是jdk7,但是现在已经更新到jdk10了,好多最新版本最新版本的jar包都是基于最新的jdk编写,所以可以

#error: Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version

昨天编译文件时出现了Building MFC application with /MD[d] (CRT dll version)requires MFC shared dll version~~~~的错误。   在网上很容易找到了解决的方案,公布如下:   对着你的项目点击右键,依次选择:属性、配置属性、常规,然后右边有个“项目默认值”,下面有个MFC的使用,选择“在共享 DLL 中使

EasyConnect 现实 Harfbuzz version too old 解决方案

https://www.cnblogs.com/cocode/p/12890684.html

Centos配置双网卡绑定(bond)

本文使用bond6模式,如需要配置其他模式请自行了解一下修改配置文件即可 编辑bond文件 cat > ifcfg-bond0 << EOFDEVICE=bond0TYPE=BondIPADDR=172.1.1.10NETMASK=255.255.255.0GATEWAY=172.1.1.1USERCTL=noBOOTPROTO=noneONBOOT=yesBONDI