uva11008 - 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).

  总共有n棵树,给出树的坐标,打一枪能打掉一条直线上的所有树,最少打几枪能打完m棵树。

  虽然坐标范围很大,但是是不影响的,因为点的个数很少。用s[i][j]记下点i和j这条直线上点的状态就和坐标没关系了,然后是记忆化搜索。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int s[20][20],d[150000],x[20],y[20];
int T,M,N,test=0;
int DP(int S){
    int &ans=d[S];
    if(ans!=-1) return ans;
    ans=INF;
    int i,j,cnt=0;
    for(i=0;i<N;i++){
        if(S&(1<<i)) cnt++;
    }
    if(cnt<=N-M) return ans=0;
    if(cnt<=2) return ans=1;
    for(i=0;i<N;i++)
    for(j=i+1;j<N;j++){
        if(S&(1<<i)&&S&(1<<j))
            ans=min(ans,DP(S&(~s[i][j]))+1);
    }
    return ans;
}
int main(){
    freopen("in.txt","r",stdin);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        memset(d,-1,sizeof(d));
        memset(s,0,sizeof(s));
        int i,j,k;
        for(i=0;i<N;i++) scanf("%d%d",&x[i],&y[i]);
        for(i=0;i<N;i++){
            s[i][i]|=1<<i;
            for(j=i+1;j<N;j++){
                for(k=0;k<N;k++) if((x[k]-x[j])*(y[j]-y[i])==(x[j]-x[i])*(y[k]-y[j])){
                    s[i][j]|=1<<k;
                }
            }
        }
        printf("Case #%d:\n",++test);
        printf("%d\n",DP((1<<N)-1));
        if(T) puts("");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值