zoukankan      html  css  js  c++  java
  • HDU

    It’s harvest season now! 
    Farmer John plants a lot of corn. There are many birds living around his corn field. These birds keep stealing his corn all the time. John can't stand with that any more. He decides to put some scarecrows in the field to drive the birds away. 
    John's field can be considered as an N×N grid which has N×N intersections. John plants his corn on every intersection at first. But as time goes by, some corn were destroyed by rats or birds so some vacant intersections were left. Now John wants to put scarecrows on those vacant intersections and he can put at most one scarecrow on one intersection. Because of the landform and the different height of corn, every vacant intersections has a scaring range R meaning that if John put a scarecrow on it, the scarecrow can only scare the birds inside the range of manhattan distance R from the intersection. 



    The figure above shows a 7×7 field. Assuming that the scaring range of vacant intersection (4,2) is 2, then the corn on the marked intersections can be protected by a scarecrow put on intersection (4,2). 
    Now John wants to figure out at least how many scarecrows he must buy to protect all his corn.

    InputThere are several test cases. 
    For each test case: 
    The first line is an integer N ( 2 <= N <= 50 ) meaning that John's field is an N×N grid. 
    The second line is an integer K ( 0<= K <= 10) meaning that there are K vacant intersections on which John can put a scarecrow. 
    The third line describes the position of K vacant intersections, in the format of r 1,c 1,r 2,c 2 …. r K,c k . (r i,c i) is the position of the i-th intersection and 1 <= r 1,c 1,r 2,c 2 …. r K,c k <= N. 
    The forth line gives the scaring range of all vacant intersections, in the format of R 1,R 2…R K and 0 <= R 1,R 2…R K <= 2 × N. 
    The input ends with N = 0.OutputFor each test case, print the minimum number of scarecrows farmer John must buy in a line. If John has no way to protect all the corn, print -1 instead.Sample Input

    4
    2
    2 2 3 3
    1 3
    4
    2
    2 2 3 3
    1 4
    0

    Sample Output

    -1
    1

    原来状态压缩就是二进制啊。
    这题注意读题,距离不是半径,而是只能上下左右走过去,走一次算一步。
    用二进制状态压缩最多也就2^10.美滋滋

    附ac代码:
      1 #include <cstdio>
      2 #include <iostream>
      3 #include <cmath>
      4 #include <string>
      5 #include <cstring>
      6 #include <algorithm>
      7 using namespace std;
      8 const int maxn = 111;
      9 typedef long long ll;
     10 const int inf = 0x3f3f3f3f;
     11 int mp[maxn][maxn];
     12 struct nod
     13 {
     14     int x,y;
     15     int r;
     16 }nu[maxn];
     17 int vis[maxn];
     18 int n,k;
     19 int a,b;
     20 int c;
     21 double getd(int x1,int x2,int y1,int y2)
     22 {
     23     double ans=abs(x1-x2)+abs(y1-y2);
     24     return ans;
     25 }
     26 int main() {
     27     while(scanf("%d",&n),n)
     28     {
     29         memset(vis,0,sizeof(vis));
     30         memset(mp,0,sizeof(mp));
     31         scanf("%d",&k);
     32         
     33         for(int i=0;i<k;++i)
     34         {
     35             scanf("%d%d",&a,&b);
     36             nu[i].x=a;
     37             nu[i].y=b;
     38             mp[a][b]=1;
     39         }
     40         for(int i=0;i<k;++i)
     41         {
     42             scanf("%d",&nu[i].r);
     43         }
     44         int len=(1<<k);
     45         int minn=inf;
     46         for(int i=0;i<len;++i)
     47         {
     48             memset(mp,0,sizeof(mp));
     49             memset(vis,0,sizeof(vis));
     50             for(int u=0;u<k;++u)
     51                 mp[nu[u].x][nu[u].y]=1;
     52             for(int j=0;j<k;++j)
     53             {
     54                 
     55                 vis[j]=i&(1<<j);
     56            //     printf("%di %d(1<<j) %dj %dvis
    ",i,(i<<j)&1,j,vis[j]);
     57                 
     58                 if(vis[j])
     59                 {
     60                     
     61                     for(int u=1;u<=n;++u)
     62                     {
     63                         for(int v=1;v<=n;++v)
     64                         {
     65                             if(!mp[u][v] && nu[j].r>=getd(nu[j].x,u,nu[j].y,v))
     66                             {
     67                                 mp[u][v]=1;
     68                             }
     69                         }
     70                     }
     71                 }
     72             }
     73             int flag=0;
     74             for(int u=1;u<=n;++u)
     75             {
     76                 for(int v=1;v<=n;++v)
     77                 {
     78                     if(!mp[u][v])
     79                     {
     80                         flag=1;
     81                         break;
     82                     }
     83                 }
     84                 if(flag==1)
     85                     break;
     86             }
     87             int cnt=0;
     88             if(!flag)
     89             {
     90                 for(int u=0;u<k;++u)
     91                 {
     92                     if(vis[u])
     93                     {
     94                         cnt++;
     95                //         printf("%dcnt
    ",cnt);
     96                     }
     97                 }
     98              //   printf("%d %d
    ",minn,cnt);
     99                 minn=min(cnt,minn);
    100             }
    101         }
    102         if(minn!=inf)
    103         printf("%d
    ",minn);
    104         else printf("-1
    ");
    105     }
    106     return 0;
    107 }
    View Code
  • 相关阅读:
    15行CSS代码攻击会导致 iOS 系统重启或 Mac 冻结
    css3特效_CSS3弹跳Loading加载动画特效的实现
    Web前端设计排版小技巧
    webpack配置sass模块的加载
    前后端分离项目的跨域及保持Session会话
    css盒子模型_css全局设置border-box
    大厂前端推荐纯css实现气泡效果
    前端开发常用css动画代码
    VSCode调试网页JavaScript代码
    HTML连载77-3D播放器
  • 原文地址:https://www.cnblogs.com/zmin/p/8335330.html
Copyright © 2011-2022 走看看