NOIP2018Day2T1奶酪 DFS+BFS+并查集

题目描述
现有一块大奶酪,它的高度为 h,它的长度和宽度我们可以认为是无限大的,奶酪 中间有许多 半径相同 的球形空洞。我们可以在这块奶酪中建立空间坐标系,在坐标系中, 奶酪的下表面为z=0,奶酪的上表面为z=h。
现在,奶酪的下表面有一只小老鼠 Jerry,它知道奶酪中所有空洞的球心所在的坐 标。如果两个空洞相切或是相交,则 Jerry 可以从其中一个空洞跑到另一个空洞,特别 地,如果一个空洞与下表面相切或是相交,Jerry 则可以从奶酪下表面跑进空洞;如果 一个空洞与上表面相切或是相交,Jerry 则可以从空洞跑到奶酪上表面。
位于奶酪下表面的 Jerry 想知道,在 不破坏奶酪 的情况下,能否利用已有的空洞跑 到奶酪的上表面去?
空间内两点P1​(x1​,y1​,z1​)、P2(x2​,y2​,z2​)的距离公式如下:
dist(P1​,P2​)=sqrt((x1​−x2​)2+(y1​−y2​)2+(z1​−z2​)2​)

输入输出格式
输入格式:
每个输入文件包含多组数据。
输入文件的第一行,包含一个正整数 T,代表该输入文件中所含的数据组数。
接下来是 T 组数据,每组数据的格式如下: 第一行包含三个正整数n,h 和 r,两个数之间以一个空格分开,分别代表奶酪中空 洞的数量,奶酪的高度和空洞的半径。
接下来的 n 行,每行包含三个整数x,y,z,两个数之间以一个空格分开,表示空 洞球心坐标为(x,y,z)。

输出格式:
输出文件包含 T 行,分别对应 T 组数据的答案,如果在第 i 组数据中,Jerry 能从下 表面跑到上表面,则输出Yes,如果不能,则输出No (均不包含引号)。

输入输出样例
输入样例#1:

3 
2 4 1 
0 0 1 
0 0 3 
2 5 1 
0 0 1 
0 0 4 
2 5 2 
0 0 2 
2 0 4

输出样例#1:

Yes
No
Yes

说明
【输入输出样例 1 说明】
Alt
第一组数据,由奶酪的剖面图可见:
第一个空洞在(0,0,0)与下表面相切
第二个空洞在(0,0,4)与上表面相切 两个空洞在(0,0,2)相切
输出 Yes
第二组数据,由奶酪的剖面图可见:
两个空洞既不相交也不相切
输出 No
第三组数据,由奶酪的剖面图可见:
两个空洞相交 且与上下表面相切或相交
输出 Yes
【数据规模与约定】
对于 20%的数据,n=1,1≤h,r≤10,000,坐标的绝对值不超过 10,000。
对于 40%的数据,1≤n≤8, 1≤h,r≤10,000,坐标的绝对值不超过 10,000。
对于80%的数据,1≤n≤1,000,1≤h,r≤10,000,坐标的绝对值不超过10,000。
对于 100%的数据,1≤n≤1,000,1≤h,r≤1,000,000,000,T≤20,坐标的 绝对值不超过 1,000,000,000。
这题有三种做法------BFS and DFS and 并查集

BFS
其实和下面的DFS思路是一样的
有问题看看下面的DFS吧

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
int t;
long long x[maxn];
long long y[maxn];
long long z[maxn];
vector<int>a[maxn];
int vis[maxn];

long long dist(long long x1,long long y1,long long z1,long long x2,long long y2,long long z2)
{
	return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2);
}

void bfs(int s,int e)
{
	queue<int> q;
	q.push(s);
	while(q.empty() == 0){
		int x = q.front();
		vis[x] = 1;
		if(x == e){
			printf("Yes\n");
			return;
		}
		q.pop();
		for(int i = 0; i < a[x].size(); i++){
			int v = a[x][i];
			if(vis[v] == 1)
				continue;
			vis[v] = 1;
			q.push(v);
		}
	}
	printf("No\n");	
}
void init()
{
	for(int i = 0; i < maxn; i++)vis[i] = 0;
	for(int i = 0; i < maxn; i++)a[i].clear();
}

int main()
{
	scanf("%d",&t);
	while(t--)
	{
		init();
		long long n,h,r;
		scanf("%lld%lld%lld",&n,&h,&r);
		for(int i = 1; i <= n; i++)scanf("%lld%lld%lld",&x[i],&y[i],&z[i]);
		for(int i = 1; i <= n; i++)
		{
			for(int j = i + 1; j <= n; j++)
			{
				if(dist(x[i],y[i],z[i],x[j],y[j],z[j]) <= 4 * r * r)
				{
					a[i].push_back(j);
					a[j].push_back(i);
				}
			}
		}
		for(int i = 1; i <= n; i++)
		{
			if(z[i] - r <= 0 && z[i] + r >= 0)
			{
				a[0].push_back(i);
				a[i].push_back(0);
			}
			if(z[i] + r >= h && z[i] - h <= h)
			{
				a[n + 1].push_back(i);
				a[i].push_back(n + 1);
			}
		}
		bfs(0,n + 1);
	}
	return 0;
}

DFS
首先把每个点设为均未标记过,然后搜每一个下表面的点
只要这个点可以到达上表面,f = 1,输出Yes,否则输出No
注意此题多组输入,在搜索前要把visit和f初始化

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn = 1e3 + 10;
struct node{
	ll x;ll y;ll z;
}a[maxn];int visit[maxn];
int n;ll h;ll r;int f;

int check(node x,node y)
{
	ll sum;sum = (x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y) + (x.z - y.z) * (x.z - y.z);
	if(sum <= 4 * r * r)return 1;
	return 0;
}

void dfs(int x)
{
	if(f == 1)return ;
	if(a[x].z + r >= h){f = 1;return ;}
	for(int i = 1; i <= n; i++)
	{
		if(visit[i] == 0)
		{
			if(check(a[x],a[i]) == 1)
			{
				visit[i] = 1;
				dfs(i);
			}
		}
	}
}

int main()
{
	int t;scanf("%d",&t);
	while(t--)
	{
		f = 0;
		scanf("%d%lld%lld",&n,&h,&r);
		for(int i = 1; i <= n; i++)
		{
			scanf("%lld%lld%lld",&a[i].x,&a[i].y,&a[i].z);
			visit[i] = 0;
		}
		for(int i = 1; i <= n; i++)
		{
			if(a[i].z <= r)
			{
				visit[i] = 1;
				dfs(i);
			}
		}
		if(f == 1)cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	return 0;
}

并查集
把上表面的点存一次,把下表面的点存一次,把相邻的洞父亲(fa[i])连一起,然后一个个搜上表面和下表面的点看是否为同一个父亲(即两个点连通),然后就输出Yes,否则输出No

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
typedef long long ll;
struct node{
	ll x;ll y;ll z;
}a[maxn];int n;ll h;ll r;int fa[maxn];
int start[maxn];int en[maxn];

long long get(int i,int j)
{
	return (a[i].x - a[j].x) * (a[i].x -a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y);
}

long long dis(int i,int j)
{
	return (a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y) + (a[i].z - a[j].z) * (a[i].z -a[j].z);
}

int find(int x)
{
	if(x == fa[x])return x;
	return fa[x] = find(fa[x]);
}

int main()
{
	int t;scanf("%d",&t);
	while(t--)
	{
//		for(int i = 1; i <= n; i++)fa[i] = i;
		memset(start,0,sizeof(start));int cnt_1 = 0;
		memset(en,0,sizeof(en));int cnt_2 = 0;
		scanf("%d%lld%lld",&n,&h,&r);
		for(int i = 1; i <= n; i++)fa[i] = i;
		for(int i = 1; i <= n; i++)
		{
			scanf("%lld%lld%lld",&a[i].x,&a[i].y,&a[i].z);
			if(a[i].z - r <= 0)cnt_1++,start[cnt_1] = i;
			if(a[i].z + r >= h)cnt_2++,en[cnt_2] = i;
			for(int j = 1; j < i; j++)
			{
				if(get(i,j) >= 4 * r * r)continue;
				if(dis(i,j) <= 4 * r * r)
				{
					int x = find(i);int y = find(j);
					if(x != y)fa[x] = y;
				}
			}
		}int flag = 0;
		for(int i = 1; i <= cnt_1; i++)
		{
			for(int j = 1; j <= cnt_2; j++)
			{
				if(find(start[i]) == find(en[j])){flag = 1;break;}
			}
			if(flag == 1)break;
		}
		if(flag == 1)cout << "Yes" << endl;
		else cout << "No" << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值