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 (≤), the number of crocodiles, and D, the maximum distance that James could jump. Then N lines follow, each containing the ( 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 ( 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<iostream>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = ;
const int minLen = - /;
struct Pointer{
int x,y;
}point[maxn];
int path[maxn] = {-};
bool visited[maxn] = {false};
int d[maxn],n,m; void init(){
for(int i = ; i < n; i++) d[i] = i;
} bool Jump(int u,int v){
int d1 = pow(point[u].x - point[v].x,);
int d2 = pow(point[u].y - point[v].y,);
int r = m * m;
if(r >= d1 + d2) return true;
else return false;
} int firstJump(int v){
int d1 = pow(point[v].x,);
int d2 = pow(point[v].y,);
int r = (m+7.5)*(m+7.5);
if(r >= d1 + d2) return d1+d2;
else return ;
} bool isSafe(int x) { /* 判断从当前点能否跳到岸上 */
if ((point[x].x - m <= -) || (point[x].x + m >= ) || (point[x].y - m <= -) || (point[x].y + m >= ))
return true;
return false;
} bool cmp(int x,int y){
return firstJump(d[x]) < firstJump(d[y]);
} void BFS() { /* 用bfs来判断最少要踩几个小鳄鱼才能上岸 */
int b[];
queue<int>q;
/* 将第一步能踩到的鳄鱼按距离从小到大的顺序进队列~ 因为输出结果要保证在踩的鳄鱼数量相等的情况下 输出第一步距离最短的~~*/
for (int i = ; i < n; i++) {
b[i] = i;
}
sort(b, b + n, cmp); /* 按照第一步的距离排序~~~ */
int last;
for (int i = ; i < n; i++) {
if (firstJump(b[i])) { /* 能跳上去! */
q.push(b[i]);
visited[b[i]] = true; /* 指向当前层数最后一个数~ */
last = b[i];
}
}
int step = ; /* 记录最少要跳跃的次数 */
int tail;
while (!q.empty()) {
int p = q.front();
q.pop();
if (isSafe(p)) {
int k = ;
stack<int> s;
cout << step << endl;
while (k < step) {
//cout << point[p].x << " " << point[p].y << endl;
s.push(p);
p = path[p];
k++;
}
while (!s.empty()) {
p = s.top();
s.pop();
cout << point[p].x << " " << point[p].y << endl;
}
return;
}
for (int i = ; i < n; i++) {
if (!visited[i] && Jump(p, i)) { /* 没踩过并且能跳到 */
q.push(i);
path[i] = p; /* 记得当前进队节点的父节点~ */
visited[i] = true;
tail = i; /* 指向下一层的最后一个元素 */
}
}
if (last == p) { /* 即将进入下一层~ */
step += ;
last = tail;
}
}
if (q.empty()) { /* 如果队列为空 说明没跳出去啊~ */
cout << "" << endl;
}
} int main(){
scanf("%d%d",&n,&m);
init();
for(int i = ; i < n; i++){
scanf("%d%d",&point[i].x,&point[i].y);
}
if(m >= minLen){
printf("1\n");
return ;
}
BFS();
return ;
}
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 ...
- 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 Bon ...
- 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 ...
随机推荐
- 【转】Xcode 清理存储空间
移除 Xcode 运行安装 APP 产生的缓存文件(DerivedData) 只要重新运行Xcode就一定会重新生成,而且会随着运行程序的增多,占用空间会越来越大.删除后在重新运行程序可能会稍微慢一点 ...
- sqlplus--spool基础运用
set heading offset feedback offset echo offset newp noneset termout offspool /home/orarun/scripts/da ...
- js对象排序&&倒序
按照对象的值大小排序对象 function sortObj(obj) { var arr = []; for (var i in obj) { arr.push([obj[i],i]); }; arr ...
- maven安装第三方jar包到本地仓库
添加项目依赖的时候,有些jar下载不下来,只有手动下载或安装到本地仓库了 首先下载所需要的jar,放到指定的文件夹 然后执行如下命令: mvn install:install-file -Dfile= ...
- 2018网络预选赛 徐州H 线段树+树状数组
设读入的数组是a,树状数组用来维护a数组区间和sum,线段树用来维护一个另一个数组ssum的区间和,区间每个点a[i]*(n-i+1),那么l-r的答案是l-r的ssum-(n-r)*(sum[r]- ...
- loj10098 分离的路径
传送门 分析 此题要先用tarjan求点双联通分量,注意在求解是要注意一条无向边只能走一次.求完之后我们发现原来的图会变成一棵树,对于 这棵树我们发现答案是(叶子节点数量+1)/2,实际便是每两个节点 ...
- 100722E The Bookcase
传送门 题目大意 给你一些书的高度和宽度,有一个一列三行书柜,要求放进去书后,三行书柜的高的和乘以书柜的宽度最小.问这个值最小是多少. 分析 我们可以先将所有书按照高度降序排好,这样对于每一层只要放过 ...
- CodeForces 404D Minesweeper 1D (DP)
题意:给定一个序列,*表示雷,1表示它旁边有一个雷,2表示它旁边有两个雷,0表示旁边没有雷,?表示未知,求有多少情况. 析:dp[i][j] 表示第 i 个放 j 状态,有多少种情况,然后很简单的DP ...
- 依赖注入(DI)与控制反转(IOC)
DI(依赖注入,Dependency Injection),和所谓的IoC(控制反转,Inversion of Control )是一个意思. DI是一种通过接口实现松耦合的设计模式.初学者可能会好奇 ...
- 阿里开源混沌工程工具 ChaosBlade
https://github.com/chaosblade-io/chaosblade