LightOj 1018 Brush (IV) 状压dp

题目大意: 有n个灰尘, 给出坐标, 吸尘器每次可以清理一条线上的全部灰尘, 问最少需要画几条线将全部灰尘覆盖。

一开始理解错题意了以为下一次画线的起点必须是上一次的终点Orz  后来发现每一次都可以随意确定起点终点。 所以直接就更简单了, 01代表这个灰尘是否还存在。

由于画线是任意的, 所以可以随意取一个点 然后枚举另一个点然后用这两个点画线(这里的随意具体到代码就是找到该状态的第一个1)。

实现起来并不难,代码如下:

#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
#define ls id<<1,l,mid
#define rs id<<1|1,mid+1,r
#define OFF(x) memset(x,-1,sizeof x)
#define CLR(x) memset(x,0,sizeof x)
#define MEM(x) memset(x,0x3f,sizeof x)
typedef long long ll ;
typedef pair<int,int> pii ;
const int maxn = 1e5+50 ;
const int inf = 0x3f3f3f3f ;
const int MOD = 1e9+7 ;
int T,n,is_line[20][20];
int dp[1<<16];
struct pnt{
    int x,y;
} p[20];

bool check(int a,int b,int c) {
    return (p[b].x - p[a].x) * (p[c].y - p[b].y) == (p[c].x - p[b].x) * (p[b].y - p[a].y) ;
}

void init(){
    CLR(is_line);
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            for (int k = 0; k < n; k++) {
                if (check(i,j,k)) is_line[i][j] |= 1 << k ;
            }
        }
    }
}

int dfs(int s) {
    int cnt = __builtin_popcount(s);
    if (cnt == 0) return 0;
    if (cnt <= 2) return 1;
    int &res = dp[s];
    if (res != inf) return res;
//    cout << id << "\n" ;
    int i = 0;
    while ((s >> i & 1)== 0) i++;
    for (int j = i + 1; j < n; j++) {
        if (!(s >> j & 1)) continue ;
//        cout << s1 << " " << s << "\n" ;
        res = min(res, dfs(s ^ (s & is_line[i][j])) + 1);
    }
    return res;
}

int main () {
#ifdef LOCAL
	freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//      freopen("C:\\Users\\Administrator\\Desktop\\out.txt","w",stdout);
#endif
    scanf("%d",&T);
    int cas = 1;
    while (T--) {
        MEM(dp);
        printf("Case %d: ",cas++);
//        cout << "T==" << T << "\n" ;
        scanf("%d",&n);
        for (int i = 0; i < n; i++) {
            scanf("%d%d",&p[i].x,&p[i].y);
        }
        init();
        printf("%d\n",dfs((1 << n) - 1));
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值