hdu 5031 Lines 爆搜

事实上嘞,这个线能够仅仅延伸一端

然后嘞,爆搜一次就能够

最后嘞,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).


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
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值