2012杭州赛区(浙江理工大学)J - Scaring the Birds

3 篇文章 0 订阅

2012杭州现场赛
2:08:19
5:00:00
                   
J - Scaring the Birds
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 


题意:题目很简单,就是n*n个点,然后某些点可以放稻草人,然后给每个稻草人的半径,问最少需要几个稻草人覆盖全部点,暴力枚举稻草人广搜就可以了。这里有个坑,可以放稻草人的地方是不需要覆盖的,一个下午没看到这个地方卡了3个小时,醉了。下面给代码。

#include<iostream>
#include<stack>
#include<cstring>
#include<map>
#include<string>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<utility>
using namespace std;
struct node {
	int x, y, value;
}p[15];
int  steparr[4][2] = { -1,0,1,0,0,-1,0,1 };
int vis[55][55],vis1[55][55];
int n;
queue<node>q;
bool bfs() {
	while (!q.empty()) {
		node now = q.front();
		q.pop();
		if (!now.value)
			continue;
		for (int i = 0; i < 4; i++) {
			int nowx = now.x + steparr[i][0];
			int nowy = now.y + steparr[i][1];
			if (nowx <= 0 || nowx > n || nowy <= 0 || nowy > n || vis[nowx][nowy] >= now.value - 1)
				continue;
			vis[nowx][nowy] = now.value - 1;
			q.push(node{ nowx,nowy ,vis[nowx][nowy] });
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (vis[i][j] == -1&&!vis1[i][j]) {
				return false;
			}
		}
	}
	return true;
}
int main() {
	while (scanf("%d", &n)) {
		if (!n)
			break;
		int k;
		scanf("%d", &k);
		memset(vis1, 0, sizeof(vis1));
		for (int i = 0; i < k; i++) {
			scanf("%d%d", &p[i].x, &p[i].y);
			vis1[p[i].x][p[i].y] = 1;
		}
		for (int i = 0; i < k; i++) {
			scanf("%d", &p[i].value);
		}
		int max = 15;
		for (int i = 0; i < (1 << k); i++) {
			memset(vis, -1, sizeof(vis));
			for (int j = 0; j < k; j++) {
				if (i&(1 << j)) {
					q.push(p[j]);
					vis[p[j].x][p[j].y] = p[j].value;
				}
			}
			int now = q.size();
			if (bfs() && now<max) {
				max = now;
			}
		}
		if (max == 15) {
			printf("-1\n");
		}
		else {
			printf("%d\n", max);
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值