HDU 4462 Scaring the Birds

Scaring the Birds

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


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




代码:

#include
#include
#include
using namespace std;
int n,k;
int r[55],c[5],rr[55];
int ans;
int g[55][55];
int dir[4][2]= {-1,0,0,-1,1,0,0,1};
bool cov[55][55];
struct node
{
    int x,y;
    int step;
} cur,tmp;
void bfs(int x,int y,int len)
{
    bool tmpg[55][55]= {false};
    cur.x=x;
    cur.y=y;
    cur.step=0;
    tmpg[cur.x][cur.y]=true;
    queue q;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(cur.step==len)
        {
            continue;
        }
        for(int i=0; i<4; i++)
        {
            tmp.x=cur.x+dir[i][0];
            tmp.y=cur.y+dir[i][1];
            tmp.step=cur.step+1;
            if((tmp.x>0&&tmp.x<=n)&&(tmp.y>0&&tmp.y<=n)&&!tmpg[tmp.x][tmp.y])
            {
                tmpg[tmp.x][tmp.y]=true;
                q.push(tmp);
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(tmpg[i][j])
                g[i][j]++;
        }
    }
}
void nbfs(int x,int y,int len)
{
    bool tmpg[55][55]= {false};
    cur.x=x;
    cur.y=y;
    cur.step=0;
    g[cur.x][cur.y]=true;
    queue q;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();

        if(cur.step==len)
        {
            continue;
        }
        for(int i=0; i<4; i++)
        {
            tmp.x=cur.x+dir[i][0];
            tmp.y=cur.y+dir[i][1];
            tmp.step=cur.step+1;
            if((tmp.x>0&&tmp.x<=n)&&(tmp.y>0&&tmp.y<=n)&&!tmpg[tmp.x][tmp.y])
            {
                tmpg[tmp.x][tmp.y]=true;
                q.push(tmp);
            }
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j =1; j<=n; j++)
            if(tmpg[i][j])
                g[i][j]--;
    }
}
bool is()
{
    int i,j;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
        {
            if(g[i][j]==0&&!cov[i][j])
            {
                break;
            }
        }
        if(j!=n+1)
            break;
    }
    if(i==n+1)
        return true;
    return false;
}
bool dfs(int des,int curh)
{
    if(des>ans||curh>k)
        return false;
    if(des==ans)
    {
        if(is())
            return true;
        return false;
    }
    bfs(r[curh],c[curh],rr[curh]);
    if(dfs(des+1,curh+1)) return true;
    nbfs(r[curh],c[curh],rr[curh]);
    if(dfs(des,curh+1)) return true;
    return false;
}
int main()
{
    while(scanf("%d",&n),n)
    {
        scanf("%d",&k);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
                g[i][j]=0,cov[i][j]=false;
        }
        for(int i=0; i


你可能感兴趣的:(搜索)