A - Artwork ( 并查集 )
题意:在一个n*m的房间里,有k个摄像头,每个摄像头给出坐标(x,y)和范围半径r , 小偷在左下角, 宝藏在右上角,问小偷能否成功偷到宝藏。
思路:可以把房间放到x-y坐标轴的第一象限, 这样小偷在(0,0) 宝藏zai(n,m), 我们先记录每一个圆和四周墙壁的相交情况,再通过并查集来确定圆和圆的关系, 如果圆1和上边界相交,圆2和下边界相交, 那么这个并查集的集合就把房间从竖直方向分开了, 小偷一定不能到达右上角。
代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3+10;
struct node {
int x,y,r;
int id;
int up,down,left,right;
}a[maxn];
int x,y,n;
int pre[maxn];
int via[maxn];
int root( int x )
{
if ( x!=pre[x] ) {
pre[x] = root(pre[x]);
}
return pre[x];
}
int join( int a, int b )
{
int aa = root(a);
int bb = root(b);
if ( aa!=bb ) {
pre[aa] = bb;
}
}
int isnect( node a, node b )
{
int dis = (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
if ( (a.r+b.r)*(a.r+b.r)>=dis ) {
return 1;
}
return 0;
}
int main()
{
int i,j,k;
cin >> x >> y >> n;
for ( i=0; i<n; i++ ) {
scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].r);
a[i].id = i;
a[i].up=a[i].down=a[i].left=a[i].right=0;
if ( a[i].x-a[i].r<=0 ) a[i].left = 1;
if ( a[i].x+a[i].r>=x ) a[i].right = 1;
if ( a[i].y-a[i].r<=0 ) a[i].down = 1;
if ( a[i].y+a[i].r>=y ) a[i].up = 1;
}
for ( i=0; i<n; i++ ) pre[i] = i;
for ( i=0; i<n; i++ ) {
for ( j=0; j<i; j++ ) {
if ( isnect(a[i],a[j])==1 ) {
join(i,j);
}
}
}
int isp = 1;
for ( i=0; i<n; i++ ) {
if ( via[i]==0 ) {
int nowleft=0,nowright=0,nowup=0,nowdown=0;
via[i] = 1;
int father = pre[i];
if ( a[i].left==1 ) nowleft=1;
if ( a[i].right==1 ) nowright=1;
if ( a[i].up==1 ) nowup=1;
if ( a[i].down==1 ) nowdown=1;
for ( j=i+1; j<n; j++ ) {
if ( via[j]==0 && pre[j]==father ) {
if ( a[j].left==1 ) nowleft=1;
if ( a[j].right==1 ) nowright=1;
if ( a[j].up==1 ) nowup=1;
if ( a[j].down==1 ) nowdown=1;
}
}
if ( nowleft==1&&nowdown==1 ) isp=0;
else if ( nowleft==1&&nowright==1 ) isp=0;
else if ( nowup==1&&nowright==1 ) isp=0;
else if ( nowup==1&&nowdown==1 ) isp=0;
}
}
if ( isp==1 ) cout << "S" << endl;
else cout << "N" << endl;
return 0;
}