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 ;
}

最新文章

  1. chkconfig系统服务启动设置
  2. util包下的Date与sql包下的Date之间的转换
  3. JS中的各种类型转换规则(转)
  4. 【转】用ASP.NET加密Cookie数据
  5. 解决NoSuchMethodError with Spring MutableValues异常问题
  6. RSA加密解密及数字签名Java实现--转
  7. 2016弱校联盟十一专场10.2——Around the World
  8. 20155304 2016-2017-2 《Java程序设计》第五周学习总结
  9. Azure Messaging-ServiceBus Messaging消息队列技术系列8-服务总线配额
  10. salesforce零基础学习(七十四)apex:actionRegion以及apex:actionSupport浅谈
  11. (转)通过maven,给没有pom文件的jar包生成pom文件,maven项目引入本地jar包
  12. Microsoft .NET Framework 3.5 离线安装方法 (仅适用于Win8以上的系统)
  13. 记一次php脚本memory exhausted
  14. OpenCV——颜色缩减、计时函数、访问像素
  15. [翻译] popping
  16. 第七章 用户输入和while语句
  17. 第二十次ScrumMeeting会议
  18. 【Python】python和json数据相互转换,json读取和写入,repr和eval()使用
  19. Tensorflow项目实战一:MNIST手写数字识别
  20. 【bzoj1066】: [SCOI2007]蜥蜴 图论-最大流

热门文章

  1. Java Web学习总结(2)Servlet(一)
  2. CSS中的flex布局
  3. react教程 — 组件
  4. [CSP-S模拟测试73]题解
  5. STL排序函数
  6. xshell链接linux出现SSH服务器拒绝了密码 的解决方案
  7. mysql 查询所有表以及对应的信息
  8. 116、TensorFlow变量的版本
  9. mockito测试入门学习
  10. Reciting(second)