PTA拯救007 (BFS)

在老电影“007之生死关头”(Live and Let Die)中有一个情节,007被毒贩抓到一个鳄鱼池中心的小岛上,他用了一种极为大胆的方法逃脱 —— 直接踩着池子里一系列鳄鱼的大脑袋跳上岸去!(据说当年替身演员被最后一条鳄鱼咬住了脚,幸好穿的是特别加厚的靴子才逃过一劫。)

设鳄鱼池是长宽为100米的方形,中心坐标为 (0, 0),且东北角坐标为 (50, 50)。池心岛是以 (0, 0) 为圆心、直径15米的圆。给定池中分布的鳄鱼的坐标、以及007一次能跳跃的最大距离,你需要告诉他是否有可能逃出生天。

输入格式:

首先第一行给出两个正整数:鳄鱼数量 N(≤100)和007一次能跳跃的最大距离 D。随后 N 行,每行给出一条鳄鱼的 (x,y) 坐标。注意:不会有两条鳄鱼待在同一个点上。

输出格式:

如果007有可能逃脱,就在一行中输出"Yes",否则输出"No"

输入样例 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

输出样例 1:

Yes

输入样例 2:

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

输出样例 2:

No

BFS解法

不过写代码的时候遇到了问题,自己写了个函数叫distance计算两点之间的距离,但是报了个no type named 'value_type' in 'struct node'的错误,报的莫名其妙,后来才知道c++里面有distance这个函数,这样就命名重复了,改掉名字就好了~

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<list>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
#define N 1050

typedef struct node{
	int x,y;
}Node;
Node fish[N];
int vis[N],d,n;

double dis(Node a,Node b){
	return sqrt((a.x-b.x)*(a.x-b.x) +(a.y-b.y)*(a.y-b.y));
}
queue<int> qu;
void bfs(){
	while(qu.size()){
		int e = qu.front();
		qu.pop();
		if(abs(fish[e].x-50)<=d||abs(fish[e].y-50)<=d){
			cout << "Yes" << endl;
			return ;
		}
		for(int i=0;i<n;i++){
			if(!vis[i]&&dis(fish[i],fish[e])<=d){
				qu.push(i);
				vis[i] = 1;
			}
		}
	}
	cout << "No" << endl;
}
int main() {
	cin >> n >> d;
	for(int i=0;i<n;i++){
		scanf("%d%d",&fish[i].x,&fish[i].y);
	}
	if(d>=50-7.5){
		cout << "Yes" << endl; return 0; 
	} 
	Node zero;
	zero.x = 0;zero.y = 0;
	for(int i=0;i<n;i++){
		if(dis(fish[i],zero)-7.5<=d){
			qu.push(i);
			vis[i] = 1;
		}
	}
	bfs();
	return 0;
}
发布了79 篇原创文章 · 获赞 37 · 访问量 8880

猜你喜欢

转载自blog.csdn.net/SinclairWang/article/details/104154037
BFS
今日推荐