重力坠击

在这里插入图片描述
在这里插入图片描述
思路: 核心:dfs、暴力
注意到数据范围 k ≤ 3 ,图大小只有15*15,暴力搜就完事,最多也就三层for循环嘛,225的三次方而已。
这里有个误区,可能有小伙伴会想着贪心,每次取消灭最多敌人的炸弹(一开始就wa在这~)。
如果一个图呈现左右少中间密集,炸一次最多可能将中间炸完,然后左右两边需要消耗两颗炸弹分别炸,有更优的方案,将图三分,两颗炸弹置于1/3 、2/3处可能将敌人团灭。所以贪心是错的。

//枚举
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,R;
int k;
int res=0;
int v[20];
struct node
{
    
    
    int x,y,r;
}t[200];
bool pd(int i,int j)
{
    
    
    int l=(t[j].x-t[i].x)*(t[j].x-t[i].x) + (t[j].y-t[i].y)*(t[j].y-t[i].y);
    if( (t[i].r+t[j].r) * (t[i].r+t[j].r) >= l)    return true;
    return false;
}
void dfs(int d)
{
    
    
    if(d>k)
    {
    
    
        int cnt=0;
        memset(v,0,sizeof(v));
        for(int i=1 ;i<=k ;i++)
        for(int j=1 ;j<=n ;j++)
        {
    
    
        	//上一次消灭的敌人记得去重
            if(pd(j,n+i) && !v[j] )   {
    
    v[j]=1;cnt++;}
        }
        res=max(res,cnt);
        return ;
    }
    //枚举225种情况
    for(int i=-7 ;i<=7 ;i++)
    for(int j=-7 ;j<=7 ;j++)
    {
    
    
        t[n+d].x=i;
        t[n+d].y=j;
        t[n+d].r=R;
        dfs(d+1);
    }
}
int main()
{
    
    
    ios::sync_with_stdio(false );
//    cin.tie(0);cout.tie(0);

    cin>>n>>k>>R;
    for(int i=1 ;i<=n ; i++)
        cin>>t[i].x>>t[i].y>>t[i].r;
    dfs(1);//从第一次炸开始搜
    cout<<res<<endl;
}


/*

8 3 1
-2 5 4
-5 1 1
2 4 2
-5 7 5
3 4 5
-7 4 1
-1 1 1
7 7 4

*/

猜你喜欢

转载自blog.csdn.net/m0_53688600/article/details/114449359