HDOJ 5031 Lines
2014-09-24 22:19
253 查看
枚举角度DFS。。。。
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
1
5 4
0 1 0 0 1
0 1 0 1 0
2 1 1 0 0
0 3 1 0 0
1 1 1 0 1
0 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;
}
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
1
5 4
0 1 0 0 1
0 1 0 1 0
2 1 1 0 0
0 3 1 0 0
1 1 1 0 1
0 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;
}
相关文章推荐
- HDU-5031-Lines(DFS)
- hdu 5031 Lines 爆搜
- HDOJ 5124 lines 【线段树 & 离散化】
- 【treap tree】 HDOJ 3726 && LA 5031 && UVA 1479 Graph and Queries
- hdoj 5124 lines【线段树+离散化】
- hdoj 5124 lines【】
- hdu 5031 Lines 爆搜
- hdu 5031 Lines
- hdoj 5124 lines 【离散化 + 线段树】
- HDOJ2049(错排)
- 【最短路】HDOJ 2363 Cycling
- hdoj 5289 线段树
- [HDOJ1015]Safecracker(DFS, 组合数学)
- HDOJ2009
- (HDOJ2031)进制转换
- HDOJ 题目1546 Idiomatic Phrases Game(最短路径)
- 01背包(分组) HDOJ 4341 Gold miner
- HDOJ(HDU) 2192 MagicBuilding(用Java的Map做了下)
- HDOJ2018
- HDOJ 1038