http://zerojudge.tw/ShowProblem?problemid=d591
因為炸彈數很少,所以直接暴力搜索,詳情請看Code
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int MAXM = 20, MAXN = 10;
const int INF = 1000000;
struct Element
{
int x, y, r;
};
Element bomb[MAXM];
Element enemy[MAXN];
int cover[MAXM];
int M, N;
int ALL;
bool in_range(const Element& B, const Element& E)
{
int distance = (B.x - E.x) * (B.x - E.x) + (B.y - E.y) * (B.y - E.y);
int radius_sum = (B.r + E.r) * (B.r + E.r);
return distance <= radius_sum;
}
void init()
{
cin >> M >> N;
ALL = (1 << N) - 1;
for(int i = 0; i < M; i ++)
cin >> bomb[i].x >> bomb[i].y >> bomb[i].r;
for(int i = 0; i < N; i ++)
cin >> enemy[i].x >> enemy[i].y >> enemy[i].r;
fill(cover, cover + M, 0);
for(int i = 0; i < M; i ++)
for(int j = 0; j < N; j ++)
if(in_range(bomb[i], enemy[j]))
cover[i] |= (1 << j);
}
int DFS(int now, int cnt, int range)
{
if(now == M)
{
if(range == ALL)
return cnt;
else
return INF;
}
int num1 = DFS(now + 1, cnt, range);
range |= cover[now];
int num2 = DFS(now + 1, cnt + 1, range);
return min(num1, num2);
}
void solve()
{
int bombs = DFS(0, 0, 0);
if(bombs == INF)
cout << "Impossible";
else
cout << bombs;
cout << "\n";
}
int main()
{
int T;
cin >> T;
while(T--)
{
init();
solve();
}
return 0;
}