lightoj1018 - Brush (IV)【状压dp】
2016-04-12 16:59
387 查看
[align=center]1018 - Brush (IV)[/align]
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.
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.
刚开始想的是贪心每次选出能覆盖点数最多的直线然后删除该直线上的点然后继续寻找下一次,感觉这样的到的就是最优解然后写好交了就wa了后来一想当选择的时候有多条直线覆盖的点数相同应该选取那条直线才能得到最优解?然后看了别人的思路状压dp然后就果断用for循环写了一个状压dp然后时间复杂度o(n*n*n+2^n*n*n)果断TLE了然后就记忆化搜索,之后还是TLE,然后就看了别人的思路加了两个剪枝。
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
刚开始想的是贪心每次选出能覆盖点数最多的直线然后删除该直线上的点然后继续寻找下一次,感觉这样的到的就是最优解然后写好交了就wa了后来一想当选择的时候有多条直线覆盖的点数相同应该选取那条直线才能得到最优解?然后看了别人的思路状压dp然后就果断用for循环写了一个状压dp然后时间复杂度o(n*n*n+2^n*n*n)果断TLE了然后就记忆化搜索,之后还是TLE,然后就看了别人的思路加了两个剪枝。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<list> #include<vector> #define inf 0x3f3f3f3f using namespace std; const int maxn=18; struct Node{ int x,y; }A[maxn]; int dp[1<<maxn]; bool judge(Node a,Node b,Node c){ return ((c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y))==0; } int line[maxn][maxn],n; int dfs(int state){ if(state==((1<<n)-1))return dp[state]=0;//点已经全部加上 if(dp[state]==-1)dp[state]=inf; else return dp[state]; int i,j; bool sign=false; for(i=0;i<n;++i){ if(state&(1<<i))continue;//剪枝当该点已经出现在已加上的点集中 for(j=i+1;j<n;++j){ if(state&(1<<j))continue;//同上不加此剪枝也可以过 sign=true; int s=state|line[i][j]; dp[state]=min(dfs(s)+1,dp[state]); } break; } if(i>=n)return dp[state]=inf;//剩下的边都有点已经加在了点集中 if(!sign)return dp[state]=1;//剩下的边中每条边中最多只有一个点不在以加入的边集中 return dp[state]; } int main() { int i,j,k,t,test=1,s; scanf("%d",&t); while(t--){ scanf("%d",&n); for(i=0;i<n;++i){ scanf("%d%d",&A[i].x,&A[i].y); } memset(line,0,sizeof(line)); for(i=0;i<n;++i){ for(j=i+1;j<n;++j){ for(k=0;k<n;++k){ if(judge(A[i],A[j],A[k])) line[i][j]|=(1<<k);//枚举出共线的个数 } } } memset(dp,-1,sizeof(dp)); printf("Case %d: %d\n",test++,dfs(0));//逆向搜索从没有加一条边开始 } return 0; }
相关文章推荐
- 人生的高境界 - 无所谓
- 个人家园 - 永久免费ASP个人主页空间
- L.WuBi - 发布个最好用的在线五笔编码查询、五笔拆字图解
- 用hta实现制作的无殇 - 快书V1.1打包下载了第1/2页
- Windows SP2 免激活安装版 - DeepinXP PLUS V2 测试版 下载
- 超星图书浏览器(SSReader) v4.00 Bulid 070511 - 数字图书阅览器 下载
- Convenientfox v0.0.1.3 - 基于FireFox的浏览器 下载
- GX::Transcoder v3.20.51.3687 - 音频视频文件转换全能王 下载
- 某市大型汽车网 - vip代码提供下载了
- CSS属性 - white-space 空白属性使用说明
- 测试模式 - XSL教程 - 5
- 选择模式 - XSL教程 - 2
- 匹配模式 - XSL教程 - 4
- XPath入门 - XSL教程 - 3
- 强烈推荐 - Ajax 技术资源中心
- filewipe - 商用级文件擦除工具 提供下载了
- Javascript - HTML的request类
- 关于XSL - XSL教程
- JS创建优美的页面滑动块效果 - Glider.js
- 脚本中出现 window.open() access is denied - 拒绝访问 情况一则及分析