pta 数据结构--拯救007 图+dfs

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

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

输入格式:

首先第一行给出两个正整数:鳄鱼数量 N100)和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


用邻接矩阵代替vis进行查重,一开始以为是迷宫问题

#include <bits/stdc++.h>
using namespace std;
int a[105];
int c[105][105];//起点50,50池心岛!,0-100 
const int INF=10000000;

struct point
{
	int x;
	int y;
}point[105];
int flag;
int n;

void dfs(int index,double d)
{

	if(abs(point[index].x)+d>=50||(abs(point[index].y)+d>=50))//最后跳出岸或一次性跳出去 
	flag=1;
	
	int i=index;
		for(int j=0;j<=n;j++)
		{
		if(i==j)continue;//不能跳自己 

			if(c[i][j]-d*d<=0)
			{

				index=j;
				c[j][i]=c[i][j]=INF;
				if(i!=0)
				  dfs(index,d);
					else
						dfs(index,d-7.5);
	
			}
		
		}
	
	//return false;
}
int main()
{
int d;
 flag=0;
cin>>n>>d;
point[0].x=0;
point[0].y=0;//起点为(0,0) 
for(int i=1;i<=n;i++)
{

	cin>>point[i].x>>point[i].y;

}
for(int i=0;i<=n;i++)
{
	for(int j=0;j<=n;j++)
	{
		c[j][i]=c[i][j]=pow(point[i].x-point[j].x,2)+pow(point[i].y-point[j].y,2);//邻接矩阵为两点距离 
			
	}
}

dfs(0,d+7.5);//起点是小岛,距离加7.5 

if(flag==1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}

猜你喜欢

转载自blog.csdn.net/zjyang12345/article/details/80390502