码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11008 Antimatter Ray Clearcutting(DP)

时间:2015-05-14 23:52:18      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

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 integersn (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).


至少放到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)

标签:

原文地址:http://blog.csdn.net/u013582254/article/details/45727843

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!