HDU 4462(暴力枚举)

时间:2023-03-08 16:40:07

因为题目当中的k比较小k <= 10,所以可以直接枚举,题目里面由两个trick, 一个是如果每个点都可以放稻草人的话,那么答案是0, 另外一个就是如果可以放稻草人的点不用被照到。知道了这两个基本上暴力既可以ac了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = ;
struct Point {
int x, y;
int r;
}p[maxn];
bool vis[maxn][maxn];
int cnt;
bool vis2[maxn][maxn];
bool judge(int n, int k, int num)
{
memset(vis, false, sizeof(vis));
cnt = ;
for (int i = ; i < k; i++)
if (( << i) & num)
cnt++;
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (vis2[i][j]) continue;
for (int q = ; q < k; q++)
{
if (( << q) & num)
{
if (abs(p[q + ].x - i) + abs(p[q + ].y - j) <= p[q + ].r)
{
vis[i][j] = true;
break;
}
}
}
if (!vis[i][j]) return false;
}
}
return true;
}
int main()
{
int n, k;
while (~scanf("%d", &n) && n)
{
memset(vis2, false, sizeof(vis2));
scanf("%d", &k);
for (int i = ; i <= k; i++)
scanf("%d %d", &p[i].x, &p[i].y);
for (int i = ; i <= k; i++)
scanf("%d", &p[i].r);
for (int i = ; i <= k; i++)
vis2[p[i].x][p[i].y] = true;
bool flag = true;
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
{
if (!vis2[i][j]) {
flag = false;
break;
} } if (flag)
{
printf("0\n");
continue;
}
int ans = ;
int maxx = ( << k);
for (int i = ; i < maxx; i++)
{
if (judge(n, k, i))
{
ans = min(ans, cnt);
}
}
if (ans > )
puts("-1");
else
printf("%d\n", ans);
}
return ;
}