[UVA][dp] 11008 - Antimatter Ray Clearcutting
Problem E
Antimatter Ray Clearcutting
Input: Standard Input
Output: Standard Output
It's year 2465, and you are the Chief Engineer for Glorified Lumberjacks Inc. on planet Trie. There is a number of trees that you need to cut down, and the only weapon you have is a high-powered antimatter ray that will cut through trees like butter. Fuel cells for the antimatter ray are very expensive, so your strategy is: stand somewhere in the forest and shoot the ray in some chosen direction. This will cut down all the trees that lie on the line in that direction. Given the locations of several trees and the number of trees that you are required to cut, what is the minimum number of shots that you need to fire?
Input
The first line of input gives the number of cases, N (at most 20). N test cases follow. Each one starts with 2 lines containing the integers n (the number of trees in the forest, at most 16) and m (the number of trees you need to cut, at most n). The next n lines will each give the (x,y) coordinates of a tree (integers in the range [-1000, 1000]).
Output
For each test case, output the line "Case #x:", where x is the number of the test case. On the next line, print the number of antimatter ray shots required to cut down at least m trees. Print an empty line between test cases.
Sample Input Output for Sample Input
2 4 4 0 0 0 1 1 0 1 1 9 7 0 0 1 1 0 2 2 0 2 2 3 0 3 1 3 2 3 4 |
Case #1: 2 Case #2: 2
|
Notes
In the first test case, you can cut down 4 trees by standing at (0, -1) and firing north (cutting 2 trees) and then standing at (1, -1) and again firing north (cutting 2 more trees).
In the second test case, you should stand at (3,-1) and fire north (cutting 4 trees) and then stand at (-1, -1) and fire north-east (cutting 3 more trees).
Problemsetter: Igor Naverniouk, EPS
Special Thanks: Yury Kholondyrev
題目描述:
一個雷射可以往一個方向將路徑上的樹全砍伐, 地圖上 N 棵樹, 要求至少砍 M 棵樹,
最少要砍幾次?
題目解法:
狀態壓縮剩餘可砍的樹, 然後窮舉雷射的方向。
#include <string.h>
#include <algorithm>
using namespace std;
int x[30], y[30];
int n, m;
char used[1<<16];
int dp[1<<16];
int g[30][30];
int dfs(int state, int m) {
if(m <= 0) return 0;
if(m == 1) return 1;
if(used[state]) return dp[state];
used[state] = 1;
int &ret = dp[state];
int i, j, k;
ret = 0xffff;
for(i = 0; i < n; i++) {
if((state>>i)&1) {
for(j = i+1; j < n; j++) {
if((state>>j)&1) {
int nstate = state^(state&g[i][j]), nm = m - __builtin_popcount(state&g[i][j]);
ret = min(ret, dfs(nstate, nm)+1);
}
}
}
}
return ret;
}
int main() {
int testcase, cases = 0, i, j, k;
scanf("%d", &testcase);
while(testcase--) {
scanf("%d %d", &n, &m);
for(i = 0; i < n; i++)
scanf("%d %d", &x[i], &y[i]);
memset(used, 0, sizeof(used));
memset(g, 0, sizeof(g));
for(i = 0; i < n; i++) {
for(j = i+1; j < n; j++) {
int state = 0;
for(k = 0; k < n; k++) {
if((x[i]-x[j])*(y[i]-y[k]) == (y[i]-y[j])*(x[i]-x[k]))
state |= 1<<k;
}
g[i][j] = state;
}
}
printf("Case #%d:\n%d\n", ++cases, dfs((1<<n)-1, m));
if(testcase) puts("");
}
return 0;
}