HDU-5031-Lines(DFS)

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
 

思路:枚举经过当前点的所有可能直线+最优化剪枝。

#include <cstdio>
#include <algorithm>
using namespace std;

int mp[55][55],ans,n,m,sum;

void dfs(int dep,int remain)
{
    if(dep>=ans) return;

    int i,j,p,q,cnt;

    cnt=0;
    
    for(i=0;i<n;i++) for(j=0;j<m;j++) if(mp[i][j]>cnt) cnt=mp[i][j];
    
    if(dep+cnt>=ans) return;

    if(!remain)
    {
        ans=min(ans,dep);

        return;
    }

    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(mp[i][j])
            {
                if(i==0)//经过改点的竖直直线
                {
                    for(p=i;p<n;p++) if(!mp[p][j]) break;

                    if(p==n)
                    {
                        for(p=i;p<n;p++) mp[p][j]--;

                        dfs(dep+1,remain-n);

                        for(p=i;p<n;p++) mp[p][j]++;
                    }
                }

                if(j==0)//经过改点的水平直线
                {
                    for(p=j;p<m;p++) if(!mp[i][p]) break;

                    if(p==m)
                    {
                        for(p=j;p<m;p++) mp[i][p]--;

                        dfs(dep+1,remain-m);

                        for(p=j;p<m;p++) mp[i][p]++;
                    }

                }

                for(p=i+1;p<n;p++)
                {
                    for(q=0;q<m;q++)
                    {
                        if(mp[p][q] && p!=i && q!=j)
                        {
                            int x=p-i,y=q-j;

                            cnt=1;

                            while(i+cnt*x>=0 && i+cnt*x<n && j+cnt*y>=0 && j+cnt*y<m && mp[i+cnt*x][j+cnt*y]) cnt++;

                            if(i+cnt*x>=0 && i+cnt*x<n && j+cnt*y>=0 && j+cnt*y<m) continue;//如果后面的不能连续
                            if(i-x>=0 && i-x<n && j-y>=0 && j-y<m) continue;//如果前面的不能连续
                            if(cnt<3) continue;//经过的点少于3个


                            cnt=1;

                            while(i+cnt*x>=0 && i+cnt*x<n && j+cnt*y>=0 && j+cnt*y<m)
                            {
                                mp[i+cnt*x][j+cnt*y]--;

                                cnt++;
                            }

                            mp[i][j]--;

                            dfs(dep+1,remain-cnt);

                            mp[i][j]++;

                            cnt=1;

                            while(i+cnt*x>=0 && i+cnt*x<n && j+cnt*y>=0 && j+cnt*y<m)
                            {
                                mp[i+cnt*x][j+cnt*y]++;

                                cnt++;
                            }
                        }
                    }
                }

                return;//找到一个点即可,我们要枚举的是经过它的可行直线
            }
        }
    }
}

int main()
{
    int T,i,j;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d",&n,&m);

        n++;
        m++;

        sum=0;

        for(i=0;i<n;i++)
        {
            for(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);
    }
}
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值