[dfs+水] hdu 4462 Scaring the Birds

來源:互聯網
上載者:User

標籤:io   os   ar   for   sp   on   代碼   amp   bs   

題意:

N*N的矩陣中有M個點可以放稻草人,且給覆蓋距離R

每個稻草人能覆曼哈頓距離R以內的點

問最少需要多少個稻草人

思路:

因為範圍很小,直接可以暴力

注意稻草人所在的位置是不需要被覆蓋的

代碼:

#include"cstdlib"#include"cstdio"#include"cstring"#include"cmath"#include"queue"#include"algorithm"#include"iostream"using namespace std;int map[55][55];int n,m;int ans;struct point{    int x,y,r;}p[11];void dfs(int x,int sum){    if(x==m)    {        int f=1;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                if(map[i][j]!=0) continue;                int k;                for(k=0;k<m;k++)                {                    if(map[p[k].x][p[k].y]==-1) continue;                    if(abs(i-p[k].x)+abs(j-p[k].y)<=p[k].r) break;                }                if(k==m)                {                    f=0;                    break;                }            }            if(!f) break;        }        if(f) ans=min(ans,sum);        return ;    }    dfs(x+1,sum);    map[p[x].x][p[x].y]=p[x].r;    dfs(x+1,sum+1);    map[p[x].x][p[x].y]=-1;}int main(){    while(scanf("%d",&n),n)    {        memset(p,0,sizeof(p));        memset(map,0,sizeof(map));        scanf("%d",&m);        for(int i=0;i<m;i++)        {            scanf("%d%d",&p[i].x,&p[i].y);            map[p[i].x][p[i].y]=-1;        }        for(int i=0;i<m;i++) scanf("%d",&p[i].r);        ans=99;        dfs(0,0);        printf("%d\n",ans==99?-1:ans);    }    return 0;}


[dfs+水] hdu 4462 Scaring the Birds

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.