CF698D Limak and Shooting Points

· · 个人记录

题目链接

一看数据范围,k\leq 7,直接爆搜。

但是……总不能爆搜1000^7吧

我们可以枚举每个怪物,再判断如何干掉这个怪物。

由于会出现怪物遮挡的情况,所以可以对每个i,j预处理出从i这个位置打到j这个怪需要先打死哪些怪。

假设我们想要干掉编号为x的怪物,那么我们就可以先选择从哪个位置对x进行“致命一击”,此时我们会得到若干个怪物,代表我们需要先干掉这些怪物,那么就可以继续枚举,枚举用哪些位置对这些怪物进行“致命一击”,然后递归做下去。

但是这样枚举感觉很麻烦。

我们可以先枚举一个排列,然后按照排列的顺序直接判断,具体地说就是每次需要打一个怪的时候强制钦定排列的当前位置去打这个怪。如果打这个怪之前需要先打别的怪,就递归打下去。

代码很好写。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
struct Vec{
    int x,y;
    Vec(int xx=0,int yy=0)
    {
        x=xx;
        y=yy;
    }
    long long dist()
    {
        return (long long)x*x+(long long)y*y;
    }
}a[10],p[1010];
Vec operator +(const Vec& a,const Vec& b)
{
    return Vec(a.x+b.x,a.y+b.y);
}
Vec operator -(const Vec& a,const Vec& b)
{
    return Vec(a.x-b.x,a.y-b.y);
}
//cross
long long operator *(const Vec& a,const Vec& b)
{
    return (long long)a.x*b.y-(long long)a.y*b.x;
}
//dot
long long operator ^(const Vec& a,const Vec& b)
{
    return (long long)a.x*b.x+(long long)a.y*b.y;
}
long long dis(Vec a,Vec b)
{
    return (a-b).dist();
}
int n,k;
vector<int>vec[10][1010];
void init()
{
    for(int i=1;i<=k;i++)
        for(int j=1;j<=n;j++)
        {
//          printf("%d %d:\n",i,j);
            for(int x=1;x<=n&&vec[i][j].size()<k;x++)
                if(dis(a[i],p[j])>dis(a[i],p[x])&&(p[j]-a[i])*(p[x]-a[i])==0ll&&((p[j]-a[i])^(p[x]-a[i]))>0ll)
                {
                    vec[i][j].push_back(x);
    //              printf("%d\n",x);
                }
        }
    return ;
}
int bookm[1010];//is dead
bool booka[10];
int P[10];//permutation
int tim;//time
bool check(int& now,int tar)
{
    if(now>k)return false;
    int cur=P[now];now++;
    bookm[tar]=tim;
    for(int i=0;i<vec[cur][tar].size();i++)
    {
        int x=vec[cur][tar][i];
        if(bookm[x]==tim)continue;
        if(!check(now,x))return false;
    }
    return true;
}
bool dfs(int now,int tar)
{
    if(now>k)
    {
        tim++;int o=1;
        return check(o,tar);
    }
    for(int i=1;i<=k;i++)
        if(!booka[i])
        {
            P[now]=i;
            booka[i]=true;
            if(dfs(now+1,tar))
                return true;
            booka[i]=false;
        }
    return false;
}
int main()
{
    scanf("%d %d",&k,&n);
    for(int i=1;i<=k;i++)
        scanf("%d %d",&a[i].x,&a[i].y);
    for(int i=1;i<=n;i++)
        scanf("%d %d",&p[i].x,&p[i].y);
    init();
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        ans+=dfs(1,i);
        memset(booka,false,sizeof(booka));
    }
    printf("%d\n",ans);
    return 0;
}