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 ...
随机推荐
- [SDOI2016]模式字符串
Description 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m的模式串s,其中每一位仍然是A到z的大写字母.Alice希望知道,有多 ...
- 洛谷1736(二维dp+预处理)
洛谷1387的进阶版,但很像. 1387要求是“全为1的正方形”,取dp[i][j] = min(dp[i-1][j-1], min(dp[i-1][j], dp[i][j-1]))吧?这个有“只有对 ...
- 线段树(单点更新)/树状数组 HDOJ 1166 敌兵布阵
题目传送门 /* 线段树基本功能:区间值的和,修改某个值 */ #include <cstdio> #include <cstring> #define lson l, m, ...
- 网站如何从http升级成https
基本概念: HTTP: 是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准,用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高效,使网络传输减少. HT ...
- Hadoop调度框架
大数据协作框架是一个桐城,就是Hadoop2生态系统中几个辅助的Hadoop2.x框架.主要如下: 1,数据转换工具Sqoop 2,文件搜集框架Flume 3,任务调度框架Oozie 4,大数 ...
- Vue 页面加载闪现代码问题
CSS中 [v-cloak] { display: none; } HTML中 <div v-cloak> {{ message }} </div> 显示代码主要是{{}}这个 ...
- hihocoder1744 hohahola
思路: 二分. 实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF = ...
- React-Native 开发问题整理
1.内嵌WebView,点击输入框后页面不自动上滚 <activity android:name=".MainActivity" android:label="@s ...
- java操作Excel、PDF文件
java操作Excel.PDF文件 分享者:Vashon 分享来源:CSDN博客 下面这些是在开发中用到的一些东西,有的代码贴的不是完整的,只是贴出了关于操作EXCEL的代码: jxl是一个*国人写的 ...
- SQLite – 删除表
SQLite -删除表 SQLite DROP TABLE语句用于删除一个表定义和所有相关的数据,索引.触发器.约束和规范许可表. 你使用这个命令时必须小心,因为一旦一个表被删除然后表中所有可用的信息 ...