码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5031 Lines 爆搜

时间:2014-11-12 19:48:25      阅读:271      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   ar   os   java   

其实嘞,这个线可以只延伸一端

然后嘞,爆搜一次就可以

最后嘞,600-800ms过


本弱就是弱啊,你来打我呀……

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[100][100];
int n,m,ans;
bool dfs(int step)
{
	int i,j,t,ii,jj,x,y,cnt,tx,ty;
	t=0;
	for(i=0;i<n;i++)
		t=max(t,*max_element(a[i],a[i]+m));
	if(step+t>=ans)
		return 0;
	if(t==0)
	{
		ans=step;
		return 1;
	}
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
		{
			if(a[i][j])
			{
				if(i==0)
				{
					for(ii=0;ii<n;ii++)
						if(!a[ii][j])
							break;
					if(ii==n)
					{
						for(ii=0;ii<n;ii++)
							a[ii][j]--;
						if(dfs(step+1))
							return 1;
						for(ii=0;ii<n;ii++)
							a[ii][j]++;
					}
				}
				if(j==0)
				{
					for(ii=0;ii<m;ii++)
						if(!a[i][ii])
							break;
					if(ii==m)
					{
						for(ii=0;ii<m;ii++)
							a[i][ii]--;
						if(dfs(step+1))
							return 1;
						for(ii=0;ii<m;ii++)
							a[i][ii]++;
					}
				}
				for(ii=i+1;ii<n;ii++)
					for(jj=0;jj<m;jj++)
					{
						if(a[ii][jj]&&jj!=j)
						{
							x=ii-i;
							y=jj-j;
							cnt=0;
							for(tx=i,ty=j;tx>=0&&tx<n&&ty>=0&&ty<m&&a[tx][ty];tx+=x,ty+=y)
								cnt++;
							if(tx>=0&&tx<n&&ty>=0&&ty<m||cnt<3)
								continue;
							for(tx=i,ty=j;tx>=0&&tx<n&&ty>=0&&ty<m&&a[tx][ty];tx+=x,ty+=y)
								a[tx][ty]--;
							if(dfs(step+1))
								return 1;
							for(tx=i,ty=j;tx>=0&&tx<n&&ty>=0&&ty<m;tx+=x,ty+=y)
								a[tx][ty]++;
						}
					}
				return 0;
			}
		}
}
int main()
{
	int T,i,j,cnt;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		n++;m++;
		cnt=0;
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				scanf("%d",&a[i][j]);
				cnt+=a[i][j];
			}
		ans=min(14,cnt/3);
		dfs(0);
		printf("%d\n",ans);
	}
}

Lines

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 817    Accepted Submission(s): 222


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).

bubuko.com,布布扣

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
 


hdu 5031 Lines 爆搜

标签:des   style   blog   http   io   color   ar   os   java   

原文地址:http://blog.csdn.net/stl112514/article/details/41048207

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!