csp 201303-4 无线网络

简单bfs

#include<bits/stdc++.h>
using namespace std;
#define maxn 205
typedef long long ll;
struct point
{
    ll x;
    ll y;
}a[maxn];

struct node
{
    int pos;
    int num;
    int cnt;
    node(int _pos=0,int _num=0,int _cnt=0)
    {
        pos=_pos;
        num=_num;
        cnt=_cnt;
    }
};

int n,m,k;
ll r;
bool vis[maxn];
bool judge(ll x1,ll y1,ll x2,ll y2)
{
    if(sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))<=r)
        return true;
    else
        return false;
}

int bfs()
{
    memset(vis,false,sizeof vis);
    node aa(1,1,0);
    queue<node> q;
    vis[1]=true;
    q.push(aa);
    node t;
    while(!q.empty())
    {
        t=q.front();
        q.pop();
        int p=t.pos;
        int num=t.num;
        int cnt=t.cnt;
        for(int i=2;i<=n+m;i++)
        {
            int pp=i;
            int nn=num+1;
            int c;
            if(!vis[i]&&judge(a[p].x,a[p].y,a[pp].x,a[pp].y))
            {
              if(i==2)
              {
                  return nn;
              }
              if(i>=n+1)
              {
                c=cnt+1;
                if(c>k) continue;
              }
              else
              {
                c=cnt;
              }
              node tmp(pp,nn,c);
              q.push(tmp);
              vis[pp]=true;
            }
        }
    }
}

int main()
{
    cin>>n>>m>>k>>r;
    for(int i=1;i<=n;i++)
    {
        ll x,y;
        cin>>x>>y;
        a[i].x=x;
        a[i].y=y;
    }
    for(int i=1+n;i<=n+m;i++)
    {
        ll x,y;
        cin>>x>>y;
        a[i].x=x;
        a[i].y=y;
    }
    cout<<bfs()-2<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/hzaukotete/article/details/85016115