Fat Brother Learns to Draw 超级压缩

本文探讨了一道算法问题:给定平面上的N个点(可能存在重合),求解覆盖所有点所需的最少直线数量。文章通过状态压缩和深度优先搜索(DFS)策略解决了这一问题,并分享了具体的实现细节。

Problem Description
When Fat Brother is a little child, he just shows that his talent of painting, here is one of his most famous masterpieces.

One day Fat Brother drew N points (the points may coincide) in a 2-dimension plane. Then he started to think about the following question: what’s the least number of lines he need to draw in this plane so that each point is in at least one of the lines.
Input
The first line of the date is an integer T (1 <= T <= 2000), which is the number of the text cases.

Then T cases follow, each case starts with a number N (1 <= N <= 20) indicated the number of the points. Then goes N lines, each line with two integer x, y (0 <= x, y <= 5) describe the coordinate of this point.
Output
For each case, outputs the case number first, then output the number of lines Fat Brother need to draw in this plane so that each point is in at least one of the lines.

See the sample input and output for more details.
Sample Input
6
4
1 1
2 2
3 3
4 4
3
1 1
2 2
1 0
5
0 0
1 0
2 0
0 1
0 2
8
0 0
1 0
2 0
2 1
2 2
1 2
0 2
0 1
2
1 5
2 3
3
0 0
0 0
0 0
Sample Output
Case 1: 1
Case 2: 2
Case 3: 2
Case 4: 3
Case 5: 1
Case 6: 1

解题思路:刚开始以为和小白上面的题目一样,结果一直超时。。。后面看了学长的题解才懂得了什么才叫压缩,什么才叫剪枝

首先压缩了所有的状态 (1 << 20),再用另一个数组标记所有状态下的第一个没有被包含的点,也就是没被画线的点。接着去重复的点。再用状态压缩把以任意两点确定一条直线,所有在这条直线上的点包含进去,接着dfs
dfs的时候用一个数组纪录当前状态下已经画了几条线了,另一个数组纪录这个状态是否有被扫描过
剪枝:
1。先判断当前状态有没有被扫描过,如果有的话就判断当前的解是不是比以前的更优
2.如果当前的线段已经大于等于ans了,就没必要继续下去了
3.如果剩下的点小于等于2,一条线画过去
4.枚举当前状态下的第一个没有画过的点后面的点,这些点和第一个个没画过共线

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = (1 << 20);

struct Point{
    int x, y;
    Point() {}
    Point(int xx, int yy) {
        x = xx, y = yy;
    }
};

int nbits[N], first[N], num[N], vis[N], cur_test, n;
int flag[6][6],line[20][20];
Point p[25];

void init() {
    for(int i = 0; i < N ; i++) {
        first[i] = - 1, nbits[i] = 0;
        for(int j = 0; j < 20; j++)
            if(i & (1 << j)) 
                nbits[i]++;
            else if(first[i] == -1)
                first[i] = j;
    }
}

void dfs(int &res, int mask, int cur) {
    if(vis[mask] == cur_test && num[mask] <= cur)
        return ;
    num[mask] = cur;
    vis[mask] = cur_test;
    if(cur >= res)
        return ;

    if(nbits[mask] == n) {
        res = cur;
        return ;
    }

    if(n - nbits[mask] <= 2) {
        res = cur + 1;
        return ;
    }

    int tmp = first[mask];
    for(int i = tmp + 1; i < n; i++)
        dfs(res, mask | (line[tmp][i]), cur+1);
}

void solve() {
    int ntest;
    scanf("%d", &ntest);
    memset(vis,0,sizeof(vis));
    for(cur_test = 1; cur_test <= ntest; cur_test++) {
        scanf("%d", &n);
        memset(flag,0,sizeof(flag));
        int x, y;
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &x, &y);
            flag[x][y] = 1;
        }
        n = 0;
        for(int i = 0; i <= 5; i++)
            for(int j = 0; j <= 5; j++)
                if(flag[i][j])
                    p[n++] = Point(i,j);
        int s;
        for(int i = 0; i < n; i++)
            for(int j = i + 1; j < n; j++) {
                s = (1 << i) | (1 << j);
                for(int k = 0; k < n; k++) {
                    if(k != i && k != j)
                        if((p[j].y - p[i].y) * (p[j].x - p[k].x) == (p[j].y - p[k].y) *(p[j].x - p[i].x))
                            s |= (1 << k);
                }
                line[i][j] = s;
            }
        int ans = 6;
        dfs(ans,0,0);
        printf("Case %d: %d\n", cur_test, ans);
    }
}

int main() {
    init();
    solve();
    return 0;
}
标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值