zoukankan      html  css  js  c++  java
  • hdu4462 Scaring the Birds

    Scaring the Birds

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 970    Accepted Submission(s): 329

    Problem Description
    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.
     
    Input
    There 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.
     
    Output
    For 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
     
    Source
    用一个状压就可了,直接全部枚举,保存最优解,这题要注意,有一个大坑,就是,可能为0,因为,如果全部都是空地,就直接输出0就可以了!
    #include <iostream>
    #include <string.h>
    #include <math.h>
    #include <stdio.h>
    using namespace std;
    #define K 12
    #define inf 0x4f4f4f4f
    int r[K],c[K],l[K],n,m,edge;
    int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}},visit[55][55],prime[55][55];
    int bfs(int sx,int sy,int goal)
    {
      int i,j,ans=0;
      ans=0;
      for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
        {
            if(!visit[i][j]&&fabs(i-sx)+fabs(j-sy)<=l[goal])
            {
                visit[i][j]=1;
                ans++;
            }
        }
      return ans;
    }
    bool judge(int num)
    {
        int i,ans,all=edge;
        if(all==0)
        return true;
        for(i=0;i<m;i++)
        {
            if(num&(1<<i))
            {
                all-=bfs(r[i],c[i],i);
                //printf("%d all",all);
                if(all==0)
                return true;
            }
        }
        return false;
    }
    int main()
    {
        int i,ans,j;
        while(scanf("%d",&n)!=EOF&&n)
        {
            scanf("%d",&m);
            memset(prime,0,sizeof(prime));
            edge=n*n;
            for(i=0;i<m;i++)
            {
                scanf("%d%d",&r[i],&c[i]);
                if(!prime[r[i]][c[i]])
                {
                    prime[r[i]][c[i]]=1;
                    edge--;
                }
            }
            for(i=0;i<m;i++)
            scanf("%d",&l[i]);
            int all=(1<<m)-1;
            int maxx=inf;
            for(i=0;i<=all;i++)
            {
                for(j=0;j<=n;j++)
                    for(int k=0;k<=n;k++)
                    {
                        visit[j][k]=prime[j][k];
                    }
                if(judge(i))
                {
                   ans=i;
                   int temp=0;
                   for(j=0;j<m;j++)
                   {
                    if(ans&(1<<j))
                    {
                        temp++;
                    }
                   }
                  maxx=min(temp,maxx);
                }
            }
            if(maxx!=inf)
            printf("%d
    ",maxx);
            else
            printf("-1
    ");
    
        }
        return 0;
    }
    



  • 相关阅读:
    x-www-form-urlencoded与multipart/form-data区别
    objc_msgSend method_getTypeEncoding 与 @encode
    历史文件备份,原文件已损失
    和安全有关的那些事(非对称加密、数字摘要、数字签名、数字证书、SSL、HTTPS及其他)
    HTTP权威指南 目录
    Makefile 与tab
    NSString+URLParser NSScanner
    (转)虚拟文件系统(VFS)浅析
    Linux套接字与虚拟文件系统(1):初始化和创建
    linux内核中的文件描述符(二)--socket和文件描述符
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3292258.html
Copyright © 2011-2022 走看看