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.
 

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.
 

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 动漫女仆被绑起来狂捅漫画 弟弟在家侵犯姐漫画大全 撩起嫂嫂裙子顶上去中国电影 女生勿进进必湿gif 皇后被太子强玩 引起嫂嫂裙子顶上去 婶子的哪里又紧水又多 哥哥好凶猛太大要撑坏 老师让学生趴小内内 两个阿姨一起陪我睡觉 朋友妻子来我家洗澡你家茜 老师 主动脱下内内 浪妇杨雪小说全集 同学的妻子在线播放 韩国朋友妻子在线播放 朋友坐牢替他照顾妻子在线播放 比我老婆还要正在线播放 朋友的妻子20在线播放 寂寞的妻子手机在线 朋友出差替他照顾妻子在线播放 偷朋友妻子在线播放 搞兄弟母亲中文字在线播放 隔壁的日本妻子1中文字 年轻的母亲5d整部中文字 老师强入我体内 嫂嫂 的房门没关紧 共享妻子第3季 小石玩妻子第三部 朋友母亲的味道中文版 互换朋友妻子3换 母亲的朋友2集中文免费观看 领导出差他老婆留我过夜 厨房偷上朋友妻全文阅读答案 被老公朋友强入bd 和领导回家睡了他老婆 老公在偷吃别人我在家吃他老爹 送领导回家强睡他老婆 我在老公的眼皮底下偷吃 快递员在我家要了我 30岁军嫂身体好软 当着女朋友的面睡他朋友视频