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<cstdio>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = ;
const int minLen = - 15.0/; struct Point
{
int x,y;
}point[maxn]; int path[maxn] = {};
bool vis[maxn] = {};
int n,d; void BFS();
void init(int b[]);
bool cmp(int x,int y);
int FirstJump(int v);
bool isSafe(int v);
bool Jump(int v1,int v2); int main()
{
scanf("%d%d",&n,&d);
for (int i = ; i < n; i++)
{
scanf("%d%d",&point[i].x,&point[i].y);
} if (d >= minLen)
{
printf("1\n");
}
else
{
BFS();
}
return ;
} void BFS()
{
int b[maxn];
init(b);
sort(b,b+n,cmp); queue<int> q;
int last;
int tail;
int step = ; for (int i = ; i < n; i++)
{
if(FirstJump(b[i]))
{
q.push(b[i]);
vis[b[i]] = true;
last = b[i];
}
} while(!q.empty())
{
int now = q.front();
q.pop(); if (isSafe(now))
{
int k =;
stack<int> s;
cout << step << endl; while (k < step)
{
s.push(now);
now = path[now];
k++;
} while (!s.empty())
{
now = s.top();
s.pop();
cout << point[now].x << " " << point[now].y << endl;
}
return;
} for (int i = ; i < n; i++)
{
if (!vis[i] && Jump(now,i))
{
q.push(i);
vis[i] = true;
tail = i;
path[i] = now;
}
} if (last == now)
{
last = tail;
step++;
}
} if (q.empty())
{
cout << "" << endl;
}
} void init(int b[])
{
for (int i = ; i < n; i++)
{
b[i] = i;
}
} bool cmp(int x,int y)
{
return FirstJump(x) < FirstJump(y);
} int FirstJump(int v)
{
int dis = pow(point[v].x,) + pow(point[v].y,);
int dis_Jump = pow(15.0/ + d,);
if (dis <= dis_Jump)
{
return dis;
}
else
{
return ;
}
} bool isSafe(int v)
{
int dis_safe = - d;
return abs(point[v].x) >= dis_safe || abs(point[v].y) >= dis_safe;
} bool Jump(int v1,int v2)
{
int dis_x = pow(point[v1].x-point[v2].x, );
int dis_y = pow(point[v1].y-point[v2].y, );
if (dis_x + dis_y <= d*d)
{
return true;
}
else
{
return false;
}
}
07-图5 Saving James Bond - Hard Version (30 分)的更多相关文章
- 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 ...
- pat06-图4. Saving James Bond - Hard Version (30)
06-图4. Saving James Bond - Hard Version (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作 ...
- 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 ...
- 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 ...
- Saving James Bond - Easy Version (MOOC)
06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...
- 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 - Hard Version
07-图5 Saving James Bond - Hard Version(30 分) 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 ...
随机推荐
- XML Schema 基本结构
<?xml version='1.0'?> <Schema name="cangchuSchema" metamodelVersion="4.0&quo ...
- spring Boot 学习(七、Spring Boot与开发热部署)
一.热部署在开发中我们修改一个Java文件后想看到效果不得不重启应用,这导致大量时间 花费,我们希望不重启应用的情况下,程序可以自动部署(热部署).有以下四 种情况,如何能实现热部署.•1.模板引擎 ...
- Schnorr签名介绍
Schnorr签名介绍 来源 https://panzhibiao.com/2019/02/28/schnorr-sigature/ https://github.com/bitcoin/bitcoi ...
- ios、安卓前端兼容性
1.日期兼容性 解决方法(请看我上一篇文章)安卓.ios时间转换成时间戳的形式 2.input框聚焦,ios出现outline或者阴影,安卓显示正常 解决方法 input:focus{outline: ...
- Vue学习之路由vue-router传参及嵌套小结(十)
一.路由传递参数: 1.使用query传值: <!DOCTYPE html> <html lang="en"> <head> <meta ...
- FreePascal - CodeTyphon 和 Lazarus, 如何像Delphi一样有代码之间的连线?
CodeTyphon 6.9 默认CodeTyphon没有开启代码之间连线功能,一直不知道如何调出来,在使用CodeTyphon 和 Lazarus 很渴望也能像Delphi那样有这种功能,实际上它们 ...
- Java 重写 super关键字
方法名.参数个数和参数类型都相同 举例 class A{ public void num() { System.out.println("A num"); } } class B ...
- jsp 获取后端配置文件.properties的某个配置内容
如后端有个叫做config.properties的配置文件: sys.img=st_sp jsp中引用的方式是: <%@ page language="java" impor ...
- 一个tomcat同时部署多个项目
一个tomcat同时部署多个项目 1. 注意事项: 1. 每一个service的端口号不能产生冲突 2. service的name属性的值可以重复 name="Catalina" ...
- RabbitMQ基本概念(一)-RabbitMQ的优劣势及产生背景
本篇并没有直接讲到技术,例如没有先写个Helloword.我想在选择了解或者学习一门技术之前先要明白为什么要现在这个技术而不是其他的,以免到最后发现自己学错了.同时如果已经确定就是他,最好先要了解下技 ...