Gym 102346A. Artwork (并查集)

题目大意:给定一个矩形和圆心在矩形内的若干圆,求能否避开圆从左下角走到右下角。

思路:并查集

考虑无法走的情况

Gym 102346A. Artwork (并查集)

 如图四种情况,当上下、左右、左下、右下被联通时,则无法走到终点

每个圆看作点i,只要判断每个圆与四个边的相交情况和圆与圆之间的相交情况即可。

#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <list>
#include <queue>
#include <vector>
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long ll;
#define _for(i,a,b) for(int i=(a) ;i<=(b) ;i++)
#define _rep(i,a,b) for(int i=(a) ;i>=(b) ;i--)
#define scd(v) scanf("%d",&v)
#define scdd(a,b) scanf("%d %d",&a,&b)
#define endl "\n"
#define IOS ios::sync_with_stdio(false)
#define pb push_back
#define all(v) v.begin(),v.end()
#define int long long
#define odd(x) x&1
#define mst(v,a) memset(v,a,sizeof(v))
#define lson p<<1 ,l,mid
#define rson p<<1|1,mid+1,r
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define pii pair<int,int>
#define pdd pair<double,double>
#define inf 0x3f3f3f3f
const int N=1e5+10;
int n,m,k;
int xx[N],yy[N],zz[N];
int fa[N];
int find(int x){return fa[x] == x?x:find(fa[x]);}
void merge(int x, int y){fa[find(x)]=find(y);}
bool pd(int i ,int j)
{
    if( (zz[i] + zz[j])*(zz[i] + zz[j]) >= (xx[i]-xx[j])*(xx[i]-xx[j]) + (yy[i]-yy[j])*(yy[i]-yy[j])) return true;
    return false;
}
signed main()
{
    //!!
//   freopen("data.txt","r",stdin);
//    !!
    IOS;
    cin>>m>>n>>k;
    _for(i,0,k+10) fa[i]=i;
    _for(i,1,k)
    {
        int x,y,z;
        cin>>x>>y>>z;
        xx[i]=x;
        yy[i]=y;
        zz[i]=z;
        int up = y + z;
        int down = y - z;
        int left = x - z;
        int ri = x + z;
        if( up >= n && n >= down  ) merge(k+1,i);//up
        if( up >= 0 && 0 >= down ) merge(k+2,i);//down
        if( ri >= 0 && 0 >= left ) merge(k+3,i);//left
        if( ri >= m && m>= left ) merge(k+4,i);//right
    }
    _for(i,1,k)
    _for(j,i+1,k) if(pd(i,j)) merge(i,j);
    if( find(k+1)==find(k+2) || find(k+1)==find(k+4) || find(k+2) == find(k+3) || find(k+3) == find(k+4))
    {
        cout<<"N"<<endl;
    }
    else cout<<"S"<<endl;
}



上一篇:AcWing 787. 归并排序


下一篇:2021牛客多校4_Inverse Pair