Saving James Bond - Hard Version
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
#include<iostream>
#include<vector>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
#define Maxnodenum 101
#define nolimitmax 100000
int flag=;//为了标志第一次拓展外层
vector<double> dist(Maxnodenum,nolimitmax);//为了记住跳到每个点的步数
vector<int> path(Maxnodenum,-);//为了记录路径
struct vertex{
int x;
int y;
};
struct graph{
int Nv;
int jump;
vertex G[Maxnodenum];
};
using Graph=graph*;
Graph BuildGraph(){
int v,x,y;
Graph gra=new graph();
cin>>gra->Nv>>gra->jump;
gra->G[].x=gra->G[].y=;
for(v=;v<=gra->Nv;v++)
{
cin>>gra->G[v].x>>gra->G[v].y;
}
return gra;
}
double distance(vertex n1,vertex n2){
return sqrt((n1.x-n2.x)*(n1.x-n2.x)+(n1.y-n2.y)*(n1.y-n2.y));
}
int saved(Graph gra,int v){
if(gra->G[v].x>=-gra->jump||gra->G[v].x<=-+gra->jump||gra->G[v].y>=-gra->jump||gra->G[v].y<=-+gra->jump)
{while(path[v]!=-){ if(path[v]==) { return ;}
v=path[v];
}
}
return ;
}
bool point(Graph gra,int v){
int x=gra->G[v].x; int y=gra->G[v].y;
if(x*x+y*y<=7.5*7.5||x>=||x<=-||y>=||y<=-)
{return false;}
else {return true;
}
}
void save(Graph gra){
if(gra->jump>=){
cout<<; return;
}
dist[]=; int v=,v1;
queue<int> q;
q.push(v);
while(!q.empty()){
v=q.front(); q.pop();
if(flag==){
for(v1=;v1<=gra->Nv;v1++){
if(point(gra,v1)&&distance(gra->G[],gra->G[v1])-7.5<=gra->jump&&path[v1]==-)
{ q.push(v1); dist[v1]=dist[v]+; path[v1]=v;}
}}
flag++;
if(flag!=){
for(v1=;v1<=gra->Nv;v1++)
if(point(gra,v1)&&distance(gra->G[v],gra->G[v1])<=gra->jump&&path[v1]==-)
{ q.push(v1); dist[v1]=dist[v]+; path[v1]=v;}}
}
int minstep=,laststep,v2;
stack<int> s;
for(v=;v<=gra->Nv;v++){
if(saved(gra,v)){
if(dist[v]<minstep){
minstep=dist[v];
laststep=v;
}else if(dist[v]==minstep){
v2=v;
while(path[v2]!=)
v2=path[v2];
v1=laststep;
while(path[v1]!=)
v1=path[v1];
laststep=distance(gra->G[],gra->G[v2])<distance(gra->G[],gra->G[v1])?v:laststep;
}}}
if(minstep!=){
v=laststep; minstep++;
cout<<minstep<<endl;
while(path[v]!=-){
s.push(v);
v=path[v];
}
while(!s.empty()){
v=s.top();
cout<<gra->G[v].x<<" "<<gra->G[v].y<<endl;
s.pop();
}
}else
cout<<<<endl;
}
int main(){
Graph gra=BuildGraph();
save(gra);
return ;
}
Saving James Bond - Hard Version的更多相关文章
- PTA 07-图5 Saving James Bond - Hard Version (30分)
07-图5 Saving James Bond - Hard Version (30分) This time let us consider the situation in the movie ...
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
- pat06-图4. Saving James Bond - Hard Version (30)
06-图4. Saving James Bond - Hard Version (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...
- pat05-图2. Saving James Bond - Easy Version (25)
05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...
- Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33
06-图2 Saving James Bond - Easy Version(25 分) This time let us consider the situation in the movie &q ...
- PAT Saving James Bond - Easy Version
Saving James Bond - Easy Version This time let us consider the situation in the movie "Live and ...
- 06-图2 Saving James Bond - Easy Version
题目来源:http://pta.patest.cn/pta/test/18/exam/4/question/625 This time let us consider the situation in ...
- PTA 06-图2 Saving James Bond - Easy Version (25分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
- 06-图2 Saving James Bond - Easy Version (25 分)
This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...
随机推荐
- Java正确URL解码方式:URLDecoder.decode
//解码,为了解决中文乱码 String str = URLDecoder.decode(request.getParameter("orderJson"),"UTF-8 ...
- Educational Codeforces Round 19 B
Description You are given sequence a1, a2, ..., an of integer numbers of length n. Your task is to f ...
- iOS常用第三方库 -转
转自 http://www.cnblogs.com/jukaiit/p/4956419.html 1.AFNetworking 轻量级的通讯类库,使用非常简单. 下载地址:https://github ...
- 【转】Android Support Library详细介绍
网上对Android Support Library中各个依赖包介绍的中文资料太少了,结合官方文档和有限的参考资料做了一次总结,有描述得不对的地方还请指正. 一.主工程.依赖包.jar包.androi ...
- Arduino中数据类型转换 float/double转换为char 亲测好使,dtostrf()函数
如何轻松玩转Arduino单片机,当我在进行数据转换的时候,遇到了问题,尝试了C语言和C++中的好多函数,都没有达到将float型数据转换为char型的目的.苦苦查阅资料后,终于找到了一个大神级函数! ...
- 8.3.3 快速系统调用 —— XP SP3上SystemCallStub的奇怪问题
依书上的例子,ReadFile()函数会调用ntdll!NtReadFile(),后者将服务号放到eax之中,然后调用SharedUserData!SystemCallStub(),由此函数执行sys ...
- COGS 1. 加法问题 (水体日常)
这是一个经典的入门问题,通过此问题,你可以学会如何使用该评测系统. [问题描述] 现在有两个实数,分别是A和B.请你从文件中读取A和B,计算它们的和A+B,并把它输出到文件中.(保留到整数) [输入格 ...
- k8s集群之Docker安装镜像加速器配置与k8s容器网络
安装Docker 参考:https://www.cnblogs.com/rdchenxi/p/10381631.html 加速器配置 参考:https://www.cnblogs.com/rdchen ...
- 请简述HTML和XHTML最重要的4点不同?
请简述HTML和XHTML最重要的4点不同? 不同: XHTML要求正确嵌套 XHTML 所有元素必须关闭 XHTML 区分大小 ...
- function语句注意事项
function语句 在Javascript中定义一个函数,有两种写法: function foo() { } 和 var foo = function () { } 两种写法完全等价.但是在解析的时 ...