HDU 4462 scaring The Birds (violence enumeration Dfs)

Source: Internet
Author: User

Topic Link: Portal

Test instructions: A n*n area, where M can put the Scarecrow, the rest are corn. For each position (x, y) The Scarecrow has a scope ri,

That is, ABS (x-i) +abs (y-j) <=r, (I,J) is within the scope of the action. Ask at least a few places to put the Scarecrow to cover all the corn, if not possible output-1.

There is a trick, is to put the Scarecrow's position without being covered

eg

Input

2 4

1 1 1 2 2 1 2 2

0 0 0 0

Output

0 0

The code is as follows:

#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <    algorithm>using namespace Std;const int maxn = 60;struct point{int x,y,wide;    Point () {} point (int _x,int _y): X (_x), Y (_y) {}}p[maxn];int mp[maxn][maxn];int n,k;bool check () {int tot=0;        for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {tot+= (mp[i][j]>0); } return tot==n*n;} BOOL Flag;void Modify (Point P,int wide,int val) {for (int i=max (1,p.x-wide), I<=min (n,p.x+wide); i++) for (int j=                Max (1,p.y-wide); J<=min (n,p.y+wide); j + +) {if (ABS (i-p.x) +abs (J-P.Y) <=wide&&mp[i][j]!=10000)        Mp[i][j]+=val;    }}void dfs (int num,int id,int tot) {if (tot>num) return;        if (tot==num) {if (check ()) flag=1;    Return } if (Flag| |    ID&GT;=K) return;    Modify (p[id],p[id].wide,1);    DFS (NUM,ID+1,TOT+1);    Modify (p[id],p[id].wide,-1); DFS (Num,id+1,tot);} int main () {while (~scanf ("%d", &n) &&n) {scanf ("%d", &k);            for (int i=0;i<k;i++) {int x, y;            scanf ("%d%d", &x,&y);            P[i]=point (x, y);        mp[x][y]=10000;        } for (int i=0;i<k;i++) {scanf ("%d", &p[i].wide);        } int ans = 100000000;            for (int i=0;i<=k;i++) {memset (Mp,0,sizeof (MP)); for (int j=0;j<k;j++) mp[p[j].x][p[j].y]=10000; '            \ ' \ flag = 0;            DFS (i,0,0);                if (flag) {ans = i;            Break        }} if (ans!=100000000) printf ("%d\n", ans);    else printf (" -1\n"); } return 0;} /***422 2 3 31 3422 2 3 31 42 41 1 1 2 2 1 2 20 0 0 0***/


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4462 scaring The Birds (violence enumeration Dfs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.