zoukankan      html  css  js  c++  java
  • 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
    ",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
     


  • 相关阅读:
    如何让我domain里的机器都跟domain controller的时间保持一致?
    [PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V
    Reboot server stuck at “Press ESC in 1 seconds to skip startup.nsh”
    [PowerShell Utils] Remotely install Hyper-V and Failover Cluster feature on a list of windows 2012 servers
    [PowerShell Utils] Automatically Change DNS and then Join Domain
    SharePoint 2016 IT Preview的新feature列表
    LeetCode Permutations问题详解
    Rotate Image 旋转图像
    单链表的快速排序(转)
    anagrams 查找序列里具有相同字符但顺序不同的单词
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/6978421.html
Copyright © 2011-2022 走看看