HDU 4462 DFS,hdu4462dfs

來源:互聯網
上載者:User

HDU 4462 DFS,hdu4462dfs

2012 Asia Hangzhou Regional Contest


給出N*N的矩陣,全部標記為0,其中有K個點標記為1,並且可以在該位置放置一個可以覆蓋曼哈頓距離為r的草人,問最少放置幾個草人,可以覆蓋所有標記為0的點

DFS即可,注意只需要覆蓋標記為0的點


#include "stdio.h"#include "string.h"int inf=0x3f3f3f3f;int n,k,ans;int x[11],y[11],w[11];int hash[11];int map[51][51];int Fabs(int a){    if (a<0) return -a;else return a;}void judge(){    int i,j,l,ok;    for (i=1;i<=n;i++)        for (j=1;j<=n;j++)        if (map[i][j]==0)        {            ok=0;            for (l=1;l<=k;l++)                if (hash[l]==1 && w[l]>=Fabs(i-x[l])+Fabs(j-y[l]))                {                    ok=1;                    break;                }            if (ok==0) return ;        }    i=0;    for (j=1;j<=k;j++)        if (hash[j]==1) i++;    if (i<ans) ans=i;}void dfs(int now){    if (now>k) { judge();return ;}    hash[now]=1;    dfs(now+1);    hash[now]=0;    dfs(now+1);}int main(){    int i;    while (scanf("%d",&n)!=EOF)    {        if (n==0) break;        scanf("%d",&k);        memset(map,0,sizeof(map));        for (i=1;i<=k;i++)        {            scanf("%d%d",&x[i],&y[i]);            map[x[i]][y[i]]=1;        }        for (i=1;i<=k;i++)            scanf("%d",&w[i]);        ans=inf;        memset(hash,0,sizeof(hash));        dfs(1);        if (ans==inf) printf("-1\n");        else printf("%d\n",ans);    }    return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.