J - Lines


题目描述:

J - Lines
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 5031
Appoint description:
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

题解:

算是好题吧= =
一共14条边让人想到用爆搜.但是14层,每层可能有很多种情况,就不敢写了…
但是分析一下,我们看一条直线的最上面的那个点,然后这一层就只需要枚举右下点,我们强制枚举第一个右下点(gcd(dx,dy)==1 或者 横着和竖着的特殊情况), 并且如果这条直线上的格子有==0的 那么就不合法,很容易不合法的. 还有,如果格子中数的最大值+已经有的直线的数目>14,那么也是不合法的,这样两个剪枝很快程序就能跑完. dfs第一次搜到的答案就是最少的答案 因为本来合法的图就不多.

重点:

爆搜的时候,每层去枚举右下点.
发现右下点有很多很多不合法的.并且放的直线越多越可能不合法.并且最终的合法状态很少
发现当前的格子最大值+已经枚举的超过了14,就不行.
两个剪枝心有点一个上界一个下界的感觉.

代码:
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 100+10;
const int limit = 14;
int a[maxn][maxn];
int n, m;
int ans;

int getMax()
{
    int t = 0;
    for(int i = 1; i<=n; i++)
    {
        for(int j = 1; j<=m; j++)
        {
            t = max(t, a[i][j]);
        }
    }
    return t;
}
int check(int x, int y)
{
    if(x>=1&&x<=n&&y>=1&&y<=m)
        return 1;
    return 0;
}
int gcd(int a, int b)
{
    if(a<0)
        a = -a;
    if(b<0)
        b = -b;
    if(b==0)
        return a;
    return gcd(b, a%b);
}

int dfs(int step)
{
    int maxA = getMax();
    if(maxA==0)
    {
        ans = step;
        return 1;
    }
    if(maxA+step > limit)
        return 0;
    int tag = 0;
    for(int i = 1; i<=n; i++)
    {
        for(int j = 1; j<=m; j++)
        {
            if(a[i][j])
            {
                int t = 100;
                for(int jj = 1; jj<=m; jj++)
                    t = min(t, a[i][jj]);
                if(t >= 1 && m >= 3)
                {
                    for(int jj = 1; jj<=m; jj++)
                    {
                        a[i][jj]--;
                    }
                    if(dfs(step+1))
                        return 1;
                    for(int jj = 1; jj<=m; jj++)
                        a[i][jj]++;
                }

                t = 100;
                for(int ii = 1; ii<=n; ii++)
                    t = min(t, a[ii][j]);
                if(t>=1 && n>=3)
                {
                    for(int ii = 1; ii<=n; ii++)
                        a[ii][j]--;
                    if(dfs(step+1))
                        return 1;
                    for(int ii = 1; ii<=n; ii++)
                        a[ii][j]++;
                }

                for(int ii = i+1; ii<=n; ii++)
                {
                    for(int jj = 1; jj<=m; jj++)
                    {
                        if(jj==j)
                            continue;
                        int dI = ii-i, dJ = jj-j;
                        if(gcd(dI, dJ)!=1)
                            continue;
                        int cnt = 0;
                        while(1)
                        {
                            if(check(i+dI*cnt,j+dJ*cnt)==0)
                                break;
                            if(a[i+dI*cnt][j+dJ*cnt]==0)
                            {
                                cnt = -1;
                                break;
                            }
                            cnt++;
                        }
                        if(cnt<3)
                            continue;

                        cnt = 0;
                        while(1)
                        {
                            if(check(i+dI*cnt,j+dJ*cnt)==0)
                                break;
                            a[i+dI*cnt][j+dJ*cnt]--;
                            cnt++;
                        }
                        if(dfs(step+1))
                            return 1;
                        cnt = 0;
                        while(1)
                        {
                            if(check(i+dI*cnt,j+dJ*cnt)==0)
                                break;
                            a[i+dI*cnt][j+dJ*cnt]++;
                            cnt++;
                        }
                    }
                }

                tag = 1;
                break;
            }
        }
        if(tag)
            break;
    }
    return 0;
}


int main()
{
   // freopen("in.txt", "r", stdin);
    int ncase;
    scanf("%d", &ncase);
    while(ncase--)
    {
        scanf("%d%d", &n, &m);
        n++;
        m++;
        for(int i = 1;i<=n;i++)
        {
            for(int j = 1;j<=m;j++)
            {
                scanf("%d", &a[i][j]);
            }
        }
        dfs(0);
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值