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 NN (\le 100≤100), the number of crocodiles, andDD, the maximum distance that James could jump. Then NN lines follow, each containing the (x, y)(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)(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

/* 解题思路: 
思路不是很难理解 但是写起来还是码量略多- -
1. 用bfs求最短路
2. path[]记录路径 压入堆栈 逆序输出即为踩过的鳄鱼~
3. 题目要求 如果有多条最短路径相同~ 那么输出第一跳最小的那条路径~~ (在bfs中将第一跳能跳的所有节点按距离从小到大一次入队即可)。
4. step记录总共跳了几次~ 这里用last记录bfs中当前层的最后一个元素 tail记录bfs中下一层的最后一个元素 每次last等于队列中弹出的元素 说明即将进入下一层
将step+1.
*/
#include "iostream"
#include "math.h"
#include "queue"
#include "stack"
#include "algorithm"
using namespace std;
int n, m;
#define MINLEN 42.5
struct Pointer {
int x;
int y;
}point[];
bool answer = false; /* 记录007能否安全逃生~~ */
bool visited[] = { false }; /* 判断当前点是否被访问过 */
int path[] = {-}; /* 记录跳跃过程中踩过的鳄鱼 */
bool isSave(int x) { /* 判断从当前点能否跳到岸上 */
if ((point[x].x - m <= -) || (point[x].x + m >= ) || (point[x].y - m <= -) || (point[x].y + m >= ))
return true;
return false;
} bool jump(int x, int y) { /* 判断2个点距离是否在跳跃能力内 */
int p1 = pow(point[x].x - point[y].x, );
int p2 = pow(point[x].y - point[y].y, );
int r = m * m;
if (p1 + p2 <= r)
return true;
return false;
} int firstJump(int x) { /* 当007处于孤岛时 第一次可以选择跳的鳄鱼 因为第一次判断能否跳跃的计算方法与后面dfs不相同 所以要单独写 */
int p1 = pow(point[x].x, );
int p2 = pow(point[x].y, );
int r = (m + 7.5) * (m + 7.5);
if (p1 + p2 <= r) {
return p1+p2;
}
return ;
}
bool cmp(int a,int b) {
return firstJump(a) < firstJump(b);
}
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 (isSave(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() {
cin >> n >> m;
for (int i = ; i < n; i++) {
cin >> point[i].x >> point[i].y;
}
if (m >= MINLEN) { /* 可以直接从孤岛上提到岸上 直接输出 */
cout << "" << endl;
return ;
}
bfs();
return ;
}

PTA 07-图5 Saving James Bond - Hard Version (30分)的更多相关文章

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

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

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

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

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

  5. pta 编程题16 Saving James Bond - Easy Version

    其它pta数据结构编程题请参见:pta 题目 主要用到了深度优先搜索. #include <iostream> using namespace std; struct Vertex { i ...

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

  7. Saving James Bond - Easy Version (MOOC)

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

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

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

  9. Saving James Bond - Hard Version

    07-图5 Saving James Bond - Hard Version(30 分) This time let us consider the situation in the movie &q ...

随机推荐

  1. HTML块

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  2. POJ 2886 Who Gets the Most Candies? 线段树

    题目: http://poj.org/problem?id=2886 左右转的果断晕,题目不难,关键是准确的转啊转.因为题目要求输出约数个数最多的数,所以预处理[1,500000]的约数的个数就行了. ...

  3. CoreGraphics --- CGContext

    CGContext又叫图形上下文,相当于一块画布,以堆栈形式存放,只有在当前context上绘图才有效.iOS有分多种图形上下文,其中UIView自带提供的在drawRect:方法中通过UIGraph ...

  4. js经验1

    1. input 获得焦点  focus(); 2.获得文档的的title _title = document.title; 3.定时检查删除dom定时器 var deleteDomInterval ...

  5. ABAP打开TCODE

    CALL FUNCTION 'TH_CREATE_MODE'  EXPORTING    transaktion    = 'ZGNBWD001'  EXCEPTIONS    max_session ...

  6. OSAL多任务资源分配机制

    转自OSAL多任务资源分配机制 一.概述      OSAL (Operating System Abstraction Layer),翻译为"操作系统抽象层". 个应用程序对象. ...

  7. Sectong日志分析

    http://tech.uc.cn/?p=2866#comments http://blog.sectong.com/blog/hw_bigdata.html

  8. JENKINS里,如何为SLAVE配置多个不同的JAVA环境?

    今天遇到这个问题了, 原来在MASTER配置里可以统一管理的,不管这个路径有没有在MASTER上. 这样一来,JENKINS在编译时,会优先选用环境变量里的JAVA版本,然后才是MAVEN里的JAVA ...

  9. 移动应用产品开发-android开发(三)

    历时一个多月的时间,这款APP算是开发完成了,最近在测试完善中,比较空闲好好总结下. 之前两次已经提到开发过程中的主要的知识点,这次主要总结下解决问题方法,http请求和安全. 首先讲下解决问题的方法 ...

  10. 【网络流24题】No.11(航空路线问题 最长不相交路径 最大费用流)

    [题意] 给定一张航空图, 图中顶点代表城市, 边代表 2 城市间的直通航线. 现要求找出一条满足下述限制条件的且途经城市最多的旅行路线.(1) 从最西端城市出发,单向从西向东途经若干城市到达最东端城 ...