PTA_2019春_075_Saving James Bond - Hard Version

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

 附上两个测试点:

17 15
10 -21
10 21
-40 10
0 -12
0 -25
35 10
0 -10
-25 22
0 -35
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10

最短路径为4,且有三个最短路径为4

最终输出的为最后一个最短路径

4
0 11
10 21
10 35

17 15
10 -21
10 21
-40 10
0 -10
0 -21
35 10
0 -10
-25 22
0 -35
-30 30
-10 22
0 11
25 21
25 10
10 10
10 35
-30 10

能出去的路径长度分别为6,4,4,3

最终输出3

3
0 -21
0 -35

 

/*本题是单源最短路径问题,无向图,无权图,并且不需要建图就可以完成*/
/*1.用队列实现找到所有出去的路径
  2.注意一步跳上岸的情况
  3.最短路径相同时,取第一步最短的*/
#include<stdio.h>
#include<stdlib.h>
/*顶点定义*/
typedef struct Data* Vertex;
struct Data {
	int x;
	int y;
};
/*队列定义*/
typedef struct Qnode* ptrtoQnode;
struct Qnode {
	int* Data;/*data数组,*/
	int front, rear;/*队列头尾指针*/
	int maxsize;/*容量*/
};
typedef ptrtoQnode Queue;
 /*好烦啊,输出路径还得用堆栈*/
typedef struct Snode* Stack; /*堆栈*/
struct Snode {
	int* data_s;
	int top;
	int maxsize;
};
/*标记数组*/
struct notes {
	int* dist;      /*这一点到起点的距离*/ 
	int* issafe;   /*该顶点是否能跳到岸上*/ 
	int* path;      /*路径*/ 
};

int AddQ(Queue Q, int x);
Queue CreateQueue(int maxsize);
int isempty(Queue Q);
int DeleteQ(Queue Q);
int issafe(int x, int y, int step);   /*判断该顶点是否可以跳到岸上*/ 
int isnext(struct Data temp, struct Data next, int step);   /*寻找该顶点的邻接点*/ 
Stack creatstack(int maxsize);
void push(Stack S, int T);
int pop(Stack S);
int isempty_s(Stack S);
int main() {
	int N;/*顶点数量*/
	int step;/*人的最大跨步*/
	scanf("%d %d", &N, &step);
	Vertex data = (Vertex)malloc(N * sizeof(struct Data));
	for (int i = 0; i < N; i++) {
		scanf("%d %d", &data[i].x, &data[i].y);
	}/*输入完毕,经测试输入无误*/

	struct notes note;
	note.dist = (int*)malloc(N * sizeof(int));  /*到起点的距离*/
	note.issafe = (int*)malloc(N * sizeof(int));  /*该点能否跳到岸上*/
	note.path = (int*)malloc(N * sizeof(int));    /*父节点是*/

	for (int i = 0; i < N; i++) {  /*对所有标记初始化*/
		note.dist[i] = -1;
		note.issafe[i] = 0;
		note.path[i]=-1;
	}

	/*开始遍历*/
	/*先将能第一步踩到的顶点入队列,将起点的所有邻接点入列*/
	if(step >=(50-7.5))
    {
        printf("1");
        return 0;
    }
	Queue Q = CreateQueue(200);
	double first_dist = (7.5 + step) * (7.5 + step);
	double  k = 7.5*7.5; 
	int dist;
	for (int i = 0; i < N; i++) {
		dist = data[i].x * data[i].x + data[i].y * data[i].y;
		if (dist < first_dist&&dist>k) { /*在人的第一步范围内的顶点*/
			note.dist[i] ++;
			note.path[i] = -1;
			AddQ(Q, i);
		}
	}
	while (isempty(Q) != 1) {
		int temp = DeleteQ(Q);
		if (issafe(data[temp].x, data[temp].y, step) == 1) {  /*该点能出去*/
			note.issafe[temp] = 1;
			note.dist[temp] ++;  /*初始的时候第一步为0,这里把第一步补上去*/
			note.dist[temp] ++; /*最后一步跳上岸*/
			continue; /*如果这个点可以跳出去,那么它的邻接点可以出去的最短距离肯定比它长,所以没有必要*/
		}
		/*当该点不是终点(能跳到岸上的点)时,则寻找下一个能跳到点集*/
		for (int i = 0; i < N; i++) {
			if (isnext(data[temp], data[i], step) == 1 && note.dist[i] == -1) { /*遍历该点的所有邻接点,即下一个能跳上的点,并且没有访问过*/
				note.dist[i] = note.dist[temp] + 1;
				note.path[i] = temp;
				AddQ(Q, i);
			}
		}
	}
	/*遍历note 的三个数组找出最短出去点*/
	int answer =-1;
	int min = 200;  /*相当于无穷值*/ 
	for (int i = 0; i < N; i++) {
		if (note.dist[i] == min&&note.issafe[i]==1) { /*可以出去的路径长度相等时,取第一步值最小的*/  /*之前一直没找到的错误发生在这里,忘记加note.issafe[i]==1这个条件*/ 
			int temp_i = i;    /*i的根顶点*/ 
			int temp_ans  = answer;   /*answer的根顶点*/ 
			while (note.path[temp_i] >= 0) {    /*寻找这两个点的根顶点,就是第一步跳的那个点*/ 
				temp_i = note.path[temp_i];
			}
			while (note.path[temp_ans] >= 0) {
				temp_ans = note.path[temp_ans];
			}
			/*选第一步最短的*/ 
			if ((data[temp_i].x*data[temp_i].x + data[temp_i].y * data[temp_i].y)< (data[temp_ans].x * data[temp_ans].x + data[temp_ans].y * data[temp_ans].y)) {
				min = note.dist[i];
				answer = i;             /*最佳点*/
			}
		}
		if (note.issafe[i] == 1&&note.dist[i]<min) {     /*可以出去的最短路径*/
			min = note.dist[i];     
			answer = i;             /*最佳点*/
		}
		
	}
	if (answer == -1) { /*没有找到出去的点*/
		printf("0");
		return 0; /*结束函数*/
	}
	printf("%d\n", min);
	Stack S = creatstack(200);
	push(S, answer);
	while (note.path[answer] >=0) {
		push(S,note.path[answer]);
		answer = note.path[answer];
	}
	while (isempty_s(S) == 0) {
		int k = pop(S);
		printf("%d %d\n",data[k].x, data[k].y);
	}
	return 0;
}
Queue CreateQueue(int maxsize) {
	Queue Q = (Queue)malloc(sizeof(struct Qnode));
	Q->Data = (int*)malloc(maxsize * sizeof(int));
	Q->front = Q->rear = 0;
	Q->maxsize = maxsize;
	return Q;
}
int AddQ(Queue Q, int x) {
	Q->rear = (Q->rear + 1) % (Q->maxsize);
	Q->Data[Q->rear] = x;
	return 1;
}
int isempty(Queue Q) {
	return(Q->rear == Q->front);
}
int DeleteQ(Queue Q) {
	Q->front = (Q->front + 1) % Q->maxsize;
	return Q->Data[Q->front];
}
int issafe(int x, int y, int step) {
	int broder;/*能出去的最小边界*/
	broder = 50 - step;
	if ((x<broder && x>(0 - broder)) && (y<broder && y>(0 - broder)))  return 0;
	else return 1;
}
int isnext(struct Data temp, struct Data next, int step) {
	if (((temp.x - next.x) * (temp.x - next.x) + (temp.y - next.y) * (temp.y - next.y)) <= step * step) return 1;
	else return 0;
}
Stack creatstack(int maxsize) {
	Stack s = (Stack)malloc(sizeof(struct Snode));
	s->data_s = (int*)malloc(maxsize * sizeof(int));
	s->top = -1;
	s->maxsize = maxsize;
	return s;
}
void push(Stack S, int T) {
	S->top++;
	S->data_s[S->top] = T;
}
int pop(Stack S) {
	return (S->data_s[S->top--]);
}
int isempty_s(Stack S) {
	return S->top == -1;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值