UVA 11008 Antimatter Ray Clearcutting(DP)

It’s year 2465, and you are the Chief Engineer for Glorified Lumberjacks Inc. on planetTrie. 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).Ntest cases follow. Each one starts with 2 lines containing theintegersn(the number of trees in the forest, at most 16) andm(the number of trees you need to cut, at mostn). The nextnlines 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:",wherexis the number of the test case. On the next line, print the number of antimatter ray shots required to cut down at leastmtrees. Print an empty line between test cases.

Sample InputOutput for Sample Input

2 4 4 0 00 1 1 0 1 19 7 0 01 10 2 2 0 2 23 0 3 1 3 2 3 4Case #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).

至少放到m棵树,每次朝一个方向射击,这个方向所有树都倒下,问你最少次数。

一开始并没有想好怎么枚举。搞了几天最后才发现枚举两点,再枚举其他点。

#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<string>#include<iostream>#include<queue>#include<cmath>#include<map>#include<stack>#include<bitset>using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair<int,int>pil;const int INF = 0x3f3f3f3f;int dp[(1<<20)+10];int x[25],y[25];int t,n,m;int num=0;bool ok(int i,int j,int k){int xx1=x[i]-x[j],yy1=y[i]-y[j];int xx2=x[j]-x[k],yy2=y[j]-y[k];return xx1*yy2==xx2*yy1;}int solve(int re,int st){if(dp[st]!=INF)return dp[st];if(re==1)return dp[st]=1;if(re<=0)return 0;for(int i=0;i<n;i++){if(st&(1<<i)) continue;for(int j=i+1;j<n;j++){if(st&(1<<j)) continue;int cnt=2,s=st;s|=(1<<i);s|=(1<<j);for(int k=0;k<n;k++){if(k==i||k==j) continue;if(st&(1<<k)) continue;if(ok(i,j,k)){s|=(1<<k);cnt++;}}dp[st]=min(dp[st],solve(re-cnt,s)+1);}}return dp[st];}int main(){int cas=1;scanf("%d",&t);while(t–){scanf("%d%d",&n,&m);REP(i,n)scanf("%d%d",&x[i],&y[i]);CLEAR(dp,INF);printf("Case #%d:\n%d\n",cas++,solve(m,0));if(t) puts("");}return 0;}/**/

,你曾经说,等我们老的时候,

UVA 11008 Antimatter Ray Clearcutting(DP)

相关文章:

你感兴趣的文章:

标签云: