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 ...
随机推荐
- 【HDOJ6609】Find the answer(线段树)
题意:给定一个n个正整数的数列,第i项为w[i],对于每个i,你要从[1,i-1]中选择一些变成0,使得变化后[1,i]的总和小于m,每次询问最少要变几个 n<=2e5,m<=1e9,1& ...
- flutter网格布局之GridView组件
前面总结了使用ListView来实现列表,但是,有的时候,数据量很大,需要使用矩阵方式排列才能更清晰的展示数据,在flutter中,可以使用网格列表组件GridView来实现这个布局. GridVie ...
- Distribution money
Distribution money Accepts: 713 Submissions: 1881 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- [杂题]:group(状压DP+轮廓线)
题目描述 $pure$在玩一个战略类游戏.现在有一个士兵方阵,每行有若干士兵,每个士兵属于某个兵种.行的顺序不可改变,且每一行中士兵的顺序也不可改变.但由于每一行都有$C$个位置($C$不小于任一行的 ...
- selenium2-java 浏览器下进行登录
完整代码实现如下: package linear; import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import ...
- appium常见问题02_android内嵌H5页(webview)如何定位
现在大多数app都是由原生页面和内嵌H5(即webview)组成,app原生页面直接定位即可,那内嵌H5页面要如何定位呢. 相信大多数人用appium做自动化时都有遇到这个问题,小编总结了下工作中该问 ...
- C++ 常见的cin的错误处理
写程序时想要用 cin 对输入的合法性检查,于是学习了一下 cin.fail() 函数,顺便学习了类似的一些函数. cin 对象包含了一个描述流状态的数据成员.流状态有 3 个标志位:eofbit,b ...
- python 简易计算器
import tkinter import tkinter.messagebox import math ''' 波波版计算器可实现的功能 1.能进行简单的加减惩处 2.能进行开根号操作 3.能进行后 ...
- 不小心执行了 rm -f,先别急着跑路
作者:justmine http://www.cnblogs.com/justmine/p/10359186.html 前言 每当我们在生产环境服务器上执行rm命令时,总是提心吊胆的,因为一不小心执行 ...
- Spark自定义维护kafka的offset到zk
import kafka.common.TopicAndPartition import kafka.message.MessageAndMetadata import kafka.serialize ...