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

2014 ACM/ICPC Asia Regional Guangzhou Online

时间: 2024-12-27 07:42:46

hdu 5031 Lines 爆搜的相关文章

HDU 4735 DLX爆搜

Little Wish~ lyrical step~ Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 417    Accepted Submission(s): 109 Problem Description N children are living in a tree with exactly N nodes, on each n

HDU 5025 水爆搜

2014 ACM/ICPC Asia Regional Guangzhou Online 水爆搜 N*N矩阵,找最长的一条路径,使'.'最多,路径可以且最多可以转一次90°. 枚举每个点,枚举8方向连续的'.'有多少个,再枚举路径方式. #include "stdio.h" #include "string.h" int main() { int n,ans,i,j,k; int sum[9]; char str[101][101]; while (scanf(&q

HDU 4403 A very hard Aoshu problem(dfs爆搜)

http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比较小,直接dfs爆搜答案即可. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<map> 6 using nam

HDU 3316 爆搜水题

爆搜水题 模拟扫雷,规则和扫雷一样 给出原图,求在X,Y位置点一下以后的图形,没有弹出的点输出-1,弹出的点输出这个点的数字 从起始点DFS一下即可 #include "stdio.h" #include "string.h" int dir[8][2]={ {-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1} }; int n; int hash[110][110]; char str[110][110]; i

hdu5355 思维+爆搜

pid=5355">http://acm.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are?m?soda and today is their birthday. The?1-st soda has prepared?n?cakes with size?1,2,-,n. Now?1-st soda wants to divide the cakes into?m?parts so that the total

8/2 multi4 E找规律+模拟,空间开小了然后一直WA。。。J爆搜check不严谨WA。。。multi3 G凸包判共线写错数组名???样例太好过.想哭jpg。

multi4 Problem E. Matrix from Arrays 题意:构造一个数组,求子矩阵前缀和. 思路:打表找规律,"发现"L为奇数时循环节为L,为偶数时循环节为2L,求相应循环节的二维前缀和然后加加减减计算一下就好. 虚伪地证明一下循环节:L为奇数时对于第x行/列开始的位置有(x  +  x+L-1)*L/2   ->     (2x+L-1)/2(为整数)*L,因此扫过L行/列也就扫过了L整数倍"(2x+L-1)/2"倍的A[0]~A[L],

Aizu 2306 Rabbit Party 爆搜顶点导出子图

题目链接:点击打开链接 题意: 给定n个点的完全图,下面给出m条边权不为0的边 下面m行给出边和边权. 其他的边边权都为0. 选择一个顶点导出子图,该子图的每个点点权为 该点连接的最小边权. 找一个这样的子图使得点权和最大,输出点权和. 思路: 因为是一个完全图,所以我们选择的点构成的图一定不包含权值为0的边.因为若包含了权值为0的边,则大可以把这两点删掉而不会减小答案. 所以我们选择的图中的边一定只 包含给出的m条边,且由这m条边构成的一个团. 再判断一下这个团中最多的点数,设最多有n个点,则

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

爆搜解hdu1572下沙小面的(2)

#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; in