PTA_2019春_062_Saving James Bond - Easy 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 whether or not he can escape.

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, print in a line "Yes" if James can escape, or "No" if not.

Sample Input 1:

14 20
25 -15
-25 28
8 49
29 15
-35 -2
5 28
27 -29
-8 -28
-20 -35
-25 -20
-13 29
-30 15
-35 40
12 12

Sample Output 1:

Yes

Sample Input 2:

4 13
-12 12
12 12
-12 -12
12 -12

Sample Output 2:

No
/* Saving James Bond - Easy Version*/
/*思路:将每个鳄鱼视为顶点,以此建立邻接表,遍历邻接表,判断能否出去
        但是不需要建立图,直接通过结构体数组存储数据,通过堆栈实现遍历*/
#include<stdio.h>
#include<stdlib.h>
/*顶点定义*/
typedef struct Data* Vertex;
struct Data {
	int x;
	int y;
};
typedef struct Snode* Stack; /*堆栈*/
struct Snode {
	int* data_s;
	int top;
	int maxsize;
};
/*堆栈操作函数*/
Stack creatstack(int maxsize);
void push(Stack S, int T);
int pop(Stack S);
int isempty(Stack S);
int issafe(int x,int y,int step);/*判断此顶点处能否上岸*/
int isnext(struct Data temp, struct Data next, int step); /*判断next是不是temp的范围之内的点*/

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);
	}/*输入完毕*/
	int* visited = (int*)malloc(N * sizeof(int));  /*标记数组,是否访问过*/
	for (int i = 0; i < N; i++) {
		visited[i] = 0;  /*初始化为0*/
	}

	/*开始遍历*/
	/*先将能第一步踩到的顶点入栈*/
	Stack S = creatstack(N);
	double first_dist = (7.5 + step) * (7.5 + step);
	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) {  /*在人的第一步范围内的顶点*/
			push(S, i);
		}
	}
	int flag = 0;/*找到出路时标记为1*/
	while (isempty(S) != 1) {
		int temp = pop(S);
		if (visited[temp] == 0) {  /*该点没有被访问过*/
			if (issafe(data[temp].x, data[temp].y, step) == 1) {
				printf("Yes");
				flag = 1;
				break;
			}
			visited[temp] = 1;
			/*当该点不是终点(能跳到岸上的点)时,则寻找下一个能跳到点集*/
			for (int i = 0; i < N; i++) {
				if (isnext(data[temp], data[i], step) == 1 && visited[i] == 0) { /*是下一个能跳上的点,并且没有访问过*/
					push(S, i);
				}
			}
		}
	}
	if (flag == 0) {  /*上述遍历后,并没有找到出路*/
		printf("No");
	}
	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(Stack S) {
	return S->top == -1;
}
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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值