HDU 4462 Scaring the Birds
来源:互联网 发布:数据核查工作总结 编辑:程序博客网 时间:2024/06/06 00:54
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.
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 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.
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
422 2 3 31 3422 2 3 31 40
Sample Output
-11
代码:
#include<iostream>#include<stdio.h>#include<queue>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<struct node> 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<struct node> 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<k; i++) { scanf("%d%d",&r[i],&c[i]); cov[r[i]][c[i]]=true; } for(int i=0; i<k; i++) { scanf("%d",&rr[i]); } if(k==n*n) { printf("0\n"); continue; } ans=1; bool flag=false; while(ans<=k) { if(dfs(0,0)) { flag=true; break; } ans++; } if(flag) printf("%d\n",ans); else printf("-1\n"); } return 0;}
- HDU 4462(Scaring the Birds)
- HDU 4462 Scaring the Birds
- HDU 4462 Scaring the Birds
- hdu 4462 Scaring the Birds
- HDU 4462 Scaring the Birds
- HDU 4462 Scaring the Birds
- HDU Scaring the Birds
- hdu 4462 Scaring the Birds 状态压缩
- [dfs+水] hdu 4462 Scaring the Birds
- hdu 4462 Scaring the Birds (暴力)
- hdu 4462 Scaring the Birds(STL)
- hdu 4462 Scaring the Birds(暴力,细读)
- hdu 4462 Scaring the Birds 回溯法
- HDU 4462 Scaring the Birds(dfs)
- HDU 4462 Scaring the Birds (枚举+暴搜)
- HDU 4462 Scaring the Birds (状态压缩 暴搞)
- HDU 4462Scaring the Birds(枚举所有状态)
- HDU 4462 Scaring the Birds (暴力枚举DFS)
- jstat的用法
- 压缩感知(Compressive Sensing)学习之(一)
- 网站性能测试PV到TPS的转换以及TPS的波动
- Json .net的用法
- Maven学习笔记
- HDU 4462 Scaring the Birds
- MVC模式下FreeMarker视图解析器与属性配置
- Linux System and Performance Monitoring(CPU篇)
- Linux System and Performance Monitoring(Memory篇)
- Linux System and Performance Monitoring(I/O篇)
- Linux System and Performance Monitoring(Network篇)
- Linux System and Performance Monitoring(总结篇)
- 得到汉字的拼音码
- 指针数组和数组指针