随笔 - 145  文章 - 0  评论 - 6  阅读 - 18万

hdu4462--曼哈顿距离

题目大意:有N*N个点的田野,然后有k个点是用来放稻草人的,每个稻草人对周围满足曼哈顿距离的庄稼有保护作用

问最小的稻草人的个数能够保护所有庄稼,如果不能保护则输出-1

注意的地方:

1.放稻草人的点不需要计算,因为不是庄稼

2.可能存在0的解,也就是k=N*N时

思路:二进制枚举所有情况,找到最小解

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
const int maxs = 51;
int n,k;
bool vis[maxs][maxs];
struct Point
{
   int x,y;
   int dis;
}point[11];
 
bool judge(Point p[],int t,Point goal)
{
    for(int i=0;i<t;i++)
        //满足曼哈顿距离的条件
        if(fabs(p[i].x-goal.x)+fabs(p[i].y-goal.y)<=p[i].dis)
            return true;
    return false;
}
 
bool solve(Point p[],int t)
{
    memset(vis,false,sizeof(vis));
    for(int i=0;i<k;i++)
        vis[point[i].x][point[i].y]=true;
    Point goal;
    for(int x=1;x<=n;x++)
        for(int y=1;y<=n;y++)
            if(!vis[x][y])
            {
                goal.x=x; goal.y=y;
                if(!judge(p,t,goal))
                    return false;
            }
    return true;
}
int main()
{
    freopen("in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF&&n)
    {
        int ans = 20;//因为最大为10
        scanf("%d",&k);
        for(int i=0;i<k;i++)
            scanf("%d%d",&point[i].x,&point[i].y);
        for(int i=0;i<k;i++)
            scanf("%d",&point[i].dis);
        if(k==n*n)
        {
            printf("0\n");
            continue;
        }
        int total = 1<<k;
        //二进制枚举
        for(int i=1;i<total;i++)
        {
            int temp;
            Point p[11];
            memset(p,0,sizeof(p));
            int cnt=0;
            for(int j=0,s=1;j<k;j++)
            {
                if(i&s)
                    p[cnt++]=point[j];
                s=s<<1;
            }
            if(solve(p,cnt))
            {
                temp=cnt;
                if(temp<ans)
                    ans=temp;
            }
        }
        if(ans==20)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
}

 

posted on   wastonl  阅读(258)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示