HDOJ 5031 Lines
来源:互联网 发布:梦幻法宝五彩娃娃数据 编辑:程序博客网 时间:2024/04/29 12:35
枚举角度DFS。。。。
Lines
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 479 Accepted Submission(s): 140
Problem Description
You play a game with your friend. He draws several lines on the paper with n×m square grids (see the left figure). After that, he writes down the number of lines passing through every integer coordinate in a matrix (see the right figure).
The number of lines passing though coordinate (i,j) is written in cell (i,j) in the right figure.(i,j both start from 0).
You are given the matrix written by your friend. You need to figure out the possible minimal number of lines your friend drew on the paper.
The number of lines passing though coordinate (i,j) is written in cell (i,j) in the right figure.(i,j both start from 0).
You are given the matrix written by your friend. You need to figure out the possible minimal number of lines your friend drew on the paper.
Input
The first line of the input contains an integer T indicating the number of test cases( 0 < T <= 10).
For each test case, the first line contains two integers n, m (1 ≤ n, m ≤ 50) representing the size of the grids on the paper. The following (n+1) × (m+1) numbers is what your friend writes. It is guaranteed that the number of lines your friend draws does not exceed 14. Each line passes through integer coordinates at least three times.
For each test case, the first line contains two integers n, m (1 ≤ n, m ≤ 50) representing the size of the grids on the paper. The following (n+1) × (m+1) numbers is what your friend writes. It is guaranteed that the number of lines your friend draws does not exceed 14. Each line passes through integer coordinates at least three times.
Output
For each test case, you need to output the minimal number of lines your friend drew on the paper in a single line.
Sample Input
15 40 1 0 0 10 1 0 1 02 1 1 0 00 3 1 0 01 1 1 0 10 1 0 1 0
Sample Output
4
Source
2014 ACM/ICPC Asia Regional Guangzhou Online
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace std;map<double,bool> DB;int n,m,ans;int mp[60][60];inline bool is_in(int x,int y){if(x>=0&&x<n&&y>=0&y<m) return true;return false;}void dfs(int deep,int remain){if(deep>=ans) return ;if(remain==0){ans=min(ans,deep);return ;}bool flag=false;for(int i=0;i<n&&!flag;i++){for(int j=0;j<m&&!flag;j++){if(mp[i][j]){flag=true;if(i==0){int jian=0;for(int p=0;p<n;p++){if(mp[p][j]) jian++;else break;}if(jian==n){for(int p=0;p<n;p++) mp[p][j]--;dfs(deep+1,remain-n);for(int p=0;p<n;p++) mp[p][j]++;}}if(j==0){int jian=0;for(int q=0;q<m;q++){if(mp[i][q]) jian++;else break;}if(jian==m){for(int q=0;q<m;q++) mp[i][q]--;dfs(deep+1,remain-m);for(int q=0;q<m;q++) mp[i][q]++;}}DB.clear();for(int p=i+1;p<n;p++){for(int q=0;q<m;q++){if(mp[p][q]&&q!=j){int x=p-i,y=q-j;double slope=y*1./x;if(DB[slope]==true) continue;DB[slope]=true;int cnt=0;while(is_in(i+cnt*x,j+cnt*y)&&mp[i+cnt*x][j+cnt*y]) cnt++;if(is_in(i-x,j-y)) continue;if(is_in(i+cnt*x,j+cnt*y)) continue;if(cnt<3) continue;for(int c=0;c<cnt;c++) mp[i+c*x][j+c*y]--;dfs(deep+1,remain-cnt);for(int c=0;c<cnt;c++) mp[i+c*x][j+c*y]++;}}}}}}}int main(){int T_T;scanf("%d",&T_T);while(T_T--){scanf("%d%d",&n,&m);n++; m++; int sum=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){scanf("%d",&mp[i][j]);sum+=mp[i][j];}}ans=min(14,sum/3);dfs(0,sum); printf("%d\n",ans);}return 0;}
0 0
- HDOJ 5031 Lines
- hdoj 5124 lines【】
- hdoj 5124lines
- HDOJ 5124 lines 【线段树 & 离散化】
- hdoj 5124 lines 【离散化 + 线段树】
- HDU-5031-Lines(DFS)
- hdu 5031 Lines 爆搜
- hdu 5031 Lines
- lines
- Lines Sample
- Defense Lines
- Intersecting Lines
- Intersecting Lines
- Intersecting Lines
- lines container
- HDUOJ lines
- bestcoder lines
- 5124 lines
- 机器学习之感知机&&python实践
- Android 监听程序的安装和卸载
- Phalcon 加密解密组件
- MapReduce工作原理图文
- 从0开始学Keil下的S3C2440裸机开发-1使用内部RAM
- HDOJ 5031 Lines
- 感受生命的伟大,努力绽放生命!
- 动态计算UITableViewCell高度详解
- 更新工艺路线资源
- maven3常用命令、java项目搭建、web项目搭建详细图解
- Android sp和wp指针
- 织梦去版权方法DedeCms 5.7全站去版权去广告方法(后台)
- A. Knight Tournament
- jsp导出到word,默认显示为web视图,应该该为页面视图