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;
}