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
我的答案(最大N没过)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h> #define QMAXSIZE 10000 struct Crocodile {
int x;
int y;
int Visited;
int Path;
};
typedef struct Crocodile *Point; //队列部分
struct QNode {
int Data[QMAXSIZE];
int rear;
int front;
};
typedef struct QNode *Queue; int IsEmpty(Queue Q)
{
return (Q->rear == Q->front);
} void AddQ(Queue PtrQ, int item)
{
if((PtrQ->rear+)%QMAXSIZE == PtrQ->front) {
printf("Queue full");
return;
}
PtrQ->rear = (PtrQ->rear+)%QMAXSIZE;
PtrQ->Data[PtrQ->rear] = item;
} int DeleteQ(Queue PtrQ)
{
if(PtrQ->front == PtrQ->rear) {
printf("Queue empty");
return -;
} else {
PtrQ->front = (PtrQ->front+)%QMAXSIZE;
return PtrQ->Data[PtrQ->front];
}
} void PrintQ(Queue PtrQ)
{
int i;
printf("[Queue]: ");
for(i=(PtrQ->front+)%QMAXSIZE;i!=(PtrQ->rear+)%QMAXSIZE
;i=(i+)%QMAXSIZE)
printf("%d ", PtrQ->Data[i]);
printf("\n");
}//end Queue void ReadPoint(Point P, int N)
{
int i;
P[].x = ;
P[].y = ;
P[].Visited = ;
P[].Path = -;
for(i=;i<N;i++) { //N=N+1
scanf("%d %d\n", &P[i].x, &P[i].y);
P[i].Visited = ;
}
} void PrintfPoint(Point P, int N)
{
int i;
for(i=;i<N;i++) { //N=N+1
printf("P[%d] X:%d Y:%d\n", i, P[i].x, P[i].y);
}
printf("----------------------------\n");
} void PrintPath(Point P, int stand)
{
int Path[], i;
// printf("[PrintPath] stand=%d\n", stand);
// if(P[stand].Path != -1) {
// PrintPath(P, P[stand].Path);
// printf("%d %d\n", P[stand].x, P[stand].y);
// }
if(stand==) printf("1\n");
else {
for(i=;P[stand].Path != -;i++) {
Path[i] = stand;
stand = P[stand].Path;
}
// printf("[PrintPaht] i=%d\n", i);
printf("%d\n", i+);
for(i--;i>=;i--) {
printf("%d %d\n", P[Path[i]].x, P[Path[i]].y);
}
}
} double PointDistance(Point P1, Point P2)
{
return sqrt(pow((P1->x - P2->x), ) + pow((P1->y - P2->y), ));
} double FindMinPath(Point P, int stand)
{
while(P[stand].Path != ) {
stand = P[stand].Path;
}
return PointDistance(&P[], &P[stand]);
} int IsUp(Point P, int stand, double D, int island)
{
int xlen = -abs(P[stand].x);
int ylen = -abs(P[stand].y);
if(island == && (xlen<=(D+7.5) || ylen<=(D+7.5)))
return ;
else if(stand!= && (xlen<=D || ylen <=D))
return ;
return ;
} int IsUseless(Point P, int stand)
{
if(abs(P[stand].x) <= 7.5 && abs(P[stand].y) <= 7.5 )
return ;
else if(abs(P[stand].x == && abs(P[stand].y == )))
return ;
else
return ;
} void Visit(Point P, int stand)
{
printf("[Point] P.x:%d y:%d\n", P[stand].x, P[stand].y);
} int BFS(Point P, int N, double D, int stand)
{
Queue Q;
int S, i, endP=-;
double dist, island = , minDist=; Q = (Queue)malloc(sizeof(struct QNode)*N);
// Visit(P, stand); //访问P[0]
P[stand].Visited = ;
AddQ(Q, stand); while(!IsEmpty(Q)) {
// PrintQ(Q);
S = DeleteQ(Q); //提取队列
if(S == ) //是否在岛上
island = ;
else
island = ;
if(IsUp(P, S, D, island)) {
endP = S;
break;
} for(i=;i<N;i++) {
if(IsUseless(P, i)) continue;
dist = PointDistance(&P[S], &P[i]);
if(!P[i].Visited && dist<=(D+(double)island*7.5)) { //未被访问且能跳到
// Visit(P, i);
P[i].Path = S;
if(IsUp(P, i, D, island)) {
// printf("[Ok] Here Point can go up\n");
// printf("[Path] \n");
// PrintPath(P, i);
// printf("\n");
double temp;
temp = FindMinPath(P, i);
if(minDist > temp) {
minDist = temp;
endP = i;
}
}
P[i].Visited = ;
AddQ(Q, i);
// dist = D+1;
}
}
}
return endP;
} int main()
{
int N, endP;
double D;
Point P;
scanf("%d %lf", &N, &D);
N++; //从1开始
P = (Point)malloc(sizeof(struct Crocodile)*N);
ReadPoint(P, N);
// PrintfPoint(P, N); endP = BFS(P, N, D, );
// printf("minP:%d\n", endP);
if(endP == -)
printf("0\n");
else {
PrintPath(P, endP);
}
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 ...
随机推荐
- Web项目改名的带来的404not found问题
为了保留上一次编辑的billsys web项目,把项目复制一份到同一个工作空间后,对原来项目名进行了重命名,如右图: 结果再去访问,一直报404错误 解决思路如下: 其实仔细观察,会在项目部署界面发现 ...
- javascript中new关键字详解
和其他高级语言一样 javascript 中也有 new 运算符,我们知道 new 运算符是用来实例化一个类,从而在内存中分配一个实例对象. 但在 javascript 中,万物皆对象,为什么还要通过 ...
- h5视频做背景的样式
video{ position: fixed; display: block; width: 100%; object-fit:fill; height:100%; right: 0px; botto ...
- (转)堆和栈的概念和区别 HeapOutOfMemory和StackOverflow解释
转:https://blog.csdn.net/pt666/article/details/70876410 https://blog.csdn.net/guohan_solft/article/de ...
- openoffice+pdf2swf+FlexPaper在线显示office和pdf
前提:本人的系统为Ubuntu 13.10 64位系统.本篇是我在配置好环境后一段时间写的,所以操作上可能会有也错误,因此仅供参考. 搜索在线显示office和pdf,最常见的方法就是把都转为swf, ...
- Nginx反向代理维基百科镜像制作全解析
近日做的Ngnx代理测试,基于Nginx的ngx_http_substitutions_filter_module模块,并利用UA跳转实现PC/移动端不同站点跳转. 1. 关于Nginx的代理详细请转 ...
- 使用 Gradle 快速创建 Java 项目
上一篇介绍了如何安装 Gradle,现在就可以直接通过已经安装好的 Gradle 创建一个普通 Java 项目 Gradle 默认内建了一个 init 插件,可以生成 Java 项目基础结构 $ gr ...
- 用shell脚本实现MongoDB数据库自动备份
一.创建MongoDB备份目录 用来存放数据 mkdir -p /data/mongodb_bak/mongodb_bak_now mkdir -p /data/mongodb_bak/mongodb ...
- WEUI官方样式小程序工具打开预览
https://github.com/Tencent/weui-wxss 用微信web开发者工具打开dist目录(请注意,是dist目录,不是整个项目)
- Kali开启SSH服务
1. 一.配置SSH参数 修改sshd_config文件,命令为: vi /etc/ssh/sshd_config 将#PasswordAuthentication no的注释去掉,并且将NO修 ...