(状压) Brush (IV) (Light OJ 1018)
Posted 栀蓝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(状压) Brush (IV) (Light OJ 1018)相关的知识,希望对你有一定的参考价值。
Mubashwir returned home from the contest and got angry after seeing his room dusty. Who likes to see a dusty room after a brain storming programming contest? After checking a bit he found an old toothbrush in his room. Since the dusts are scattered everywhere, he is a bit confused what to do. So, he called Shakib. Shkib said that, ‘Use the brush recursively and clean all the dust, I am cleaning my dust in this way!‘
So, Mubashwir got a bit confused, because it‘s just a tooth brush. So, he will move the brush in a straight line and remove all the dust. Assume that the tooth brush only removes the dusts which lie on the line. But since he has a tooth brush so, he can move the brush in any direction. So, he counts a move as driving the tooth brush in a straight line and removing the dusts in the line.
Now he wants to find the maximum number of moves to remove all dusts. You can assume that dusts are defined as 2D points, and if the brush touches a point, it‘s cleaned. Since he already had a contest, his head is messy. That‘s why he wants your help.
Input
Input starts with an integer T (≤ 1000), denoting the number of test cases.
Each case starts with a blank line. The next line contains three integers N (1 ≤ N ≤ 16). N means that there are N dust points. Each of the next N lines will contain two integers xi yi denoting the coordinate of a dust unit. You can assume that (-1000 ≤ xi, yi ≤ 1000) and all points are distinct.
Output
For each case print the case number and the minimum number of moves.
Sample Input |
Output for Sample Input |
2
3 0 0 1 1 2 2
3 0 0 1 1 2 3 |
Case 1: 1 Case 2: 2 |
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<map> using namespace std; typedef unsigned long long LL; #define met(a,b) (memset(a,b,sizeof(a))) const int INF = 1e9+7; const int maxn = 105; const int MOD = 9973; struct node { int x, y; } a[20]; int dp[(1<<17)], n; int Line[20][20]; ///Line[i][j] 代表与线段ij共线的点 int DFS(int sta) { if(dp[sta]!=-1) return dp[sta]; dp[sta] = INF; int cnt=0; for(int i=0; i<n; i++) if(sta&(1<<i)) cnt++; if(cnt==0) return dp[sta]=0; else if(cnt<=2) return dp[sta]=1; for(int i=0; i<n; i++) { if(sta&(1<<i))///第i个物品 { for(int j=i+1; j<n; j++) { int w = (sta|Line[i][j]) - Line[i][j]; dp[sta] = min(dp[sta], DFS(w)+1); } break; ///优化,只需找到sta中的一个点即可, Line[i][j]会将所有的i到i后面的点都遍历一遍的 } } return dp[sta]; } int main() { int T, iCase = 1; scanf("%d", &T); while(T --) { int i, j, k, K; scanf("%d", &n); K = (1<<n)-1; met(dp, -1); met(Line, 0); for(i=0; i<n; i++) { scanf("%d%d", &a[i].x, &a[i].y); Line[i][i] = (1<<i); } for(i=0; i<n; i++) for(j=i+1; j<n; j++) for(k=0; k<n; k++) { ///判断三点是否共线 if( (a[i].x-a[k].x)*(a[i].y-a[j].y) == (a[i].x-a[j].x)*(a[i].y-a[k].y) ) Line[i][j] += (1<<k); } printf("Case %d: %d\n", iCase++, DFS(K)); } return 0; }
先预处理出来每条线段,对每一个状态选择两个不在状态的点,然后画以两个点为端点的线,来进行状态转移
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<map> using namespace std; typedef unsigned long long LL; #define met(a,b) (memset(a,b,sizeof(a))) const int INF = 1e9+7; const int maxn = 105; const int MOD = 9973; struct node { int x, y; }a[20]; vector<int>G[(1<<17)]; int dp[(1<<17)], n; int Line[20][20]; ///Line[i][j] 代表与线段ij共线的点 int main() { int T, iCase = 1, i, j; for(i=0; i<(1<<16); i++) for(j=0; j<16; j++) { if((i&(1<<j))==0) G[i].push_back(j); } scanf("%d", &T); while(T --) { int k, K; scanf("%d", &n); K = (1<<n)-1; met(dp, INF); met(Line, 0); for(i=0; i<n; i++) { scanf("%d%d", &a[i].x, &a[i].y); Line[i][i] = (1<<i); } for(i=0; i<n; i++) for(j=i+1; j<n; j++) for(k=0; k<n; k++) { ///判断三点是否共线 if( (a[i].x-a[k].x)*(a[i].y-a[j].y) == (a[i].x-a[j].x)*(a[i].y-a[k].y) ) Line[i][j] += (1<<k); } dp[0] = 0; for(i=0; i<K; i++) { int len = G[i].size(); int x=G[i][0], y; for(j=0; j<len; j++) { y = G[i][j]; dp[i|Line[x][y]] = min(dp[i|Line[x][y]], dp[i]+1); } } printf("Case %d: %d\n", iCase++, dp[K]); } return 0; } /** 2 3 0 0 1 1 2 2 3 0 0 1 1 2 3 */
以上是关于(状压) Brush (IV) (Light OJ 1018)的主要内容,如果未能解决你的问题,请参考以下文章
Light OJ 1019 - Brush (V)(图论-dijkstra)