06-图2 Saving James Bond - Easy Version

06-图2 Saving James Bond - Easy Version

来自:PTA_数据结构_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

方法1:

#include <stdio.h>
#include <math.h>
#define N 100
#define D 15
int visited[N], n;
double cr[N][2], jump;
int check(int i);
double distance(double *a, double *b);
 
int main() {
	int i, flag = 0;
	scanf("%d %lf\n", &n, &jump);
	for (i = 0;i < n;i++) {
		scanf("%lf %lf\n", &cr[i][0], &cr[i][1]);
		visited[i] = 0;
	}
	for (i = 0;i < n;i++) { //找到James第一步能跳到的鳄鱼,对它进行DFS。
		if (!flag && !visited[i] && sqrt(pow(cr[i][0], 2) + pow(cr[i][1], 2)) <= jump + D / 2)
			flag = check(i);
	}
	if (flag) printf("Yes");
	else printf("No");
	return 0;
}
//深度优先搜索
int check(int i) {  //DFS思路:先判断当前的节点是否安全...
	int j, flag = 0;//        如果安全,程序跳出即可;
	visited[i] = 1;//         如果不安全,找当前节点相邻且未访问的节点,递归调用DFS。
	if (fabs(cr[i][0]) + jump >= 50 || fabs(cr[i][1]) + jump >= 50)
		return 1;   
	for (j = 0;j < n;j++) {
		if (!visited[j] && distance(cr[i], cr[j]) <= jump)
			flag = check(j);
	}
	return flag;
}
/*
二维数组的函数调用:(值得学习)
利用指针,把二维数组看作一维数组...
其中一维数组的每个元素占用的内存大小为:2*(int)
*/
double distance(double *a, double *b) {
	return sqrt(pow(a[0] - b[0], 2) + pow(a[1] - b[1], 2));
}

方法2:

#include<stdlib.h>
 
typedef float Elemetype;
typedef struct v {
	int n;
	struct v* next;
}V,*qV;
 
typedef struct point {
	Elemetype x;
	Elemetype y;
	int states;
}Point,*qPoint;
 
typedef struct stack {
	int *n;	//*qV的编号
	int top;
}Stack,*qStack;
 
//构造栈
void CreateStack(qStack *q,int numbers) {
	*q = (Stack *)malloc(sizeof(Stack));
	(*q)->n = (int *)malloc(sizeof(int)*numbers);
	(*q)->top = -1;
}
 
//出栈
int Pop(qStack *q) {
	int N;
	if ((*q)->top != -1) {
		N = (*q)->n[(*q)->top];
		(*q)->top--;
	}
	return N;
}
//入栈
void Push(qStack *q, int N) {
	(*q)->top++;
	(*q)->n[(*q)->top] = N;
}
 
//构造矩阵表
void Create(qV **q,int numbers) {
	*q = (qV *)malloc(sizeof(qV)*numbers);
	for (int i = 0; i < numbers; i++)
	{
		V *a = (V *)malloc(sizeof(V));
		a->n = i;
		//a->states = 0;
		a->next = NULL;
		(*q)[i] = a;
	}
}
 
//判断点集是否能连通
int isLink(Point q, Point p,float d) {
	if (p.x==0&&p.y==0) {
		float D = (q.x - p.x)*(q.x - p.x) + (q.y - p.y)*(q.y - p.y);
		if (D <= (d+15/2.0) * (d + 15/2.0)) {
			return 1;
		}
		else {
			return 0;
		}
	}
	else {
		float D = (q.x - p.x)*(q.x - p.x) + (q.y - p.y)*(q.y - p.y);
		if (D <= d * d) {
			return 1;
		}
		else {
			return 0;
		}
	}
}
 
//判断能否逃脱
int isPao(Point q,float D) {
	if (q.x >= (50 - D) || q.x <= (D - 50) || q.y >= (50-D) || q.y <= (D - 50)) {
		return 1;
	}
	else {
		return 0;
	}
}
//向矩阵表插入节点
void Insert(qV **q,int i,int n) {
	if (i != n) {
		qV s = (qV)malloc(sizeof(V));
		s->n = i;
		s->next = (*q)[n]->next;
		(*q)[n]->next = s;
	}
}
 
//构造点集
qPoint Creat(qPoint q,int number) {
	q = (Point *)malloc(sizeof(Point)*number);
	return q;
}
 
//开始遍历图
int Throgh(qStack *stack,qV *q,int numbers,qPoint p,float D) {
	int N;
	qV s;
	s = q[0];	//起点的周围点都放入栈中
	while (s->next != NULL) {
		s = s->next;
		Push(stack, s->n);
		p[(s->n) - 1].states = 1;
	}
	//栈不为空时
	while ((*stack)->top!=-1)
	{
		N = Pop(stack);
		if (isPao(p[N-1], D) == 1) {
			return 1;
		}
		s = q[N];
		while (s->next != NULL) {	//队列都入栈
			s = s->next;
			if (p[(s->n) - 1].states != 1) {
				Push(stack, (s)->n);
				p[(s->n) - 1].states = 1;
				
			}
		}
	}
	return 0;
}
 
 
int main()
{
	//读入的数以及距离
	int numbers,D;
	scanf("%d %d", &numbers, &D);
	
	//构造矩阵表
	qV *a = NULL;
	Create(&a, numbers+1);
 
	//创建栈
	qStack stack=NULL;
	CreateStack(&stack,numbers);
 
	//原点
	Point p0;
	p0.x = 0;
	p0.y = 0;
	p0.states = 0;
 
	//构造点集
	qPoint q = NULL;
	q=Creat(q,numbers);
	int X, Y;
	for (int  i = 0; i < numbers; i++)
	{
		scanf("%d %d", &X, &Y);
		q[i].x = X;
		q[i].y = Y;
		q[i].states = 0;
 
		//起始点能走的路径都插入
		if (isLink(q[i],p0 , D)==1) {
			Insert(&a, i+1, 0);
		}
 
	}
	
 
	//从第一个点开始插入能连通的路径
	for (int i = 1; i <= numbers; i++)
	{
		for (int j = 0; j < numbers; j++)
		{
			if (isLink(q[j], q[i-1], D) == 1) {
				Insert(&a, j + 1, i);
			}
		}
	}
 
	int flag= Throgh(&stack,  a,  numbers, q,  D);
	if (flag == 1) {
		printf("Yes");
	}
	else {
		printf("No");
	}
    return 0;
}
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值