zoukankan      html  css  js  c++  java
  • HDOJ 4462 Scaring the Birds

     


    Scaring the Birds

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


    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.

    HDOJ 4462 Scaring the Birds - qhn999 - 码代码的猿猿


    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 r1,c1,r2,c2 …. rK,ck . (ri,ci) is the position of the i-th intersection and 1 <= r1,c1,r2,c2…. rK,ck <= N. 
    The forth line gives the scaring range of all vacant intersections, in the format of R1,R2…RK and 0 <= R1,R2…RK <= 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
     



    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>

    using namespace std;

    const int INF=0x3f3f3f3f;

    struct node
    {
        int x,y,r;
    }p[11];

    int n,k;
    int g[55][55];

    void pt(node a,int b)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
               if(abs(i-a.x)+abs(j-a.y)<=a.r)
                  g[j]+=b;
        if(g[a.x][a.y]==0) g[a.x][a.y]=1;
    }

    bool OK()
    {
        bool ok=true;
        for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(g[j]==0) {ok=false;break;}
        return ok;
    }

    int dfs(int x)
    {
        if( OK() ) return 0;
        else if(x>=k) return INF;

        int ans1,ans2;
        ans1=dfs(x+1);

        pt(p[x],1);
        ans2=dfs(x+1)+1;
        pt(p[x],-1);

        return min(ans1,ans2);
    }

    int main()
    {
        while(cin>>n&&n)
        {
            cin>>k;
            memset(g,0,sizeof(g));
            for(int i=0;i<k;i++)
            {
                cin>>p.x>>p.y;
                g[p.x][p.y]=1;
            }
            for(int i=0;i<k;i++)
                cin>>p.r;

            int ans = dfs(0);
            if(ans==INF)
                cout<<"-1 ";
            else
                cout<<ans<<endl;
        }

        return 0;
    }
    * This source code was highlighted by YcdoiT. ( style: Pyte )
  • 相关阅读:
    layui的table使用,二
    将字符串中的以某个字符间隔放到数组中
    oracle中的 函数应用
    spring 的3种常用的注入方式
    写一个方法,输入两个正整数,输出在两个正整数范围内即被3整除,又被7整除的正整数
    Myeclipse 10.7配置egit及导入项目
    Plsqi安装
    web项目引入js包时,报syntax error on token
    java笔试面试题总结
    web.xml中的配置
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350904.html
Copyright © 2011-2022 走看看