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的更多相关文章

  1. 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 ...

  2. Saving James Bond - Easy Version (MOOC)

    06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...

  3. pat06-图4. Saving James Bond - Hard Version (30)

    06-图4. Saving James Bond - Hard Version (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...

  4. pat05-图2. Saving James Bond - Easy Version (25)

    05-图2. Saving James Bond - Easy Version (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...

  5. 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 ...

  6. PAT Saving James Bond - Easy Version

    Saving James Bond - Easy Version This time let us consider the situation in the movie "Live and ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. SQL - nulls值排序问题

    给字段排序时遇到的null值问题 当我们使用order by来为指定的字段进行排序时,如果db中该字段的值存在着null值,那么在排序时这些null值会不会参与排序呢?如果参与排序的话,又是以怎样的标 ...

  2. Maximal Area Quadrilateral CodeForces - 340B || 三点坐标求三角形面积

    Maximal Area Quadrilateral CodeForces - 340B 三点坐标求三角形面积(可以带正负,表示向量/点的不同相对位置): http://www.cnblogs.com ...

  3. 学习JavaScript数据结构与算法 (一)

    学习JavaScript数据结构与算法 的笔记, 包含一二三章 01基础 循环 斐波那契数列 var fibonaci = [1,1] for (var i = 2; i< 20;i++) { ...

  4. Contextual Action bar(3) 两个示例

    一.通过activity启动Context Action Bar 1.主java public class ActivityActionModeFrgmt extends Fragment imple ...

  5. html5新增的主题结构元素

    article元素 article元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容. 它可以是一篇博客或者报刊中的文章,一篇论坛帖子.一段用户评论或独立的插件. 或其他任何独立的 ...

  6. P1309 瑞士轮 未完成 60

    题目背景 在双人对决的竞技性比赛,如乒乓球.羽毛球.国际象棋中,最常见的赛制是淘汰赛和循环赛.前者的特点是比赛场数少,每场都紧张刺激,但偶然性较高.后者的特点是较为公平,偶然性较低,但比赛过程往往十分 ...

  7. 伟景行 citymaker 从入门到精通(2)——工程图层树加载

    工程树是指explorer左边这棵树 本例子实现了图层树加载,点击节点切换可视状态 树控件使用easyui的树 html部分 onCheck:treeProjectTreeOnCheck是指树节点的o ...

  8. 类似QQ在线离线好友界面

    把头像设置成圆形的代码如下: package com.example.lesson6_11_id19; import android.content.Context; import android.c ...

  9. Android CursorAdapter的使用

    CursorAdapter继承于BaseAdapter,为Cursor和ListView连接提供了桥梁. 首先看一下CursorAdapter的部分源码: /** * @see android.wid ...

  10. mysql 查询数据库参数命令

    1.select @@tx_isolation;    查询数据库设置的事务隔离级别 2.desc table_name;  显示表设计 3.show create table table_name; ...