码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3419 The Three Groups(回溯+减枝)

时间:2016-05-13 00:58:52      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3419

思路:注意减枝就行,不然会TLE

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
const int maxn = 100010;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int visit[10];
int a[10];
int sum;
int total = 0;
int one,two,three;
void dfs(int cur)
{
    if(cur == sum)
    {
        int sum1 = 0,sum2 = 0,sum3 = 0;
        for(int i=0; i<one; i++)
            sum1 = sum1 * 10 + a[i];
        for(int i=one; i<one + two ;i++)
            sum2 = sum2 * 10 + a[i];
        for(int i=one + two; i<one + two + three; i++)
            sum3 = sum3 * 10 + a[i];
        if(sum1 * sum2 == sum3)
            total++;
        return;
    }
    for(int i=1; i<=9; i++)
    {
        if(!visit[i])
        {
            visit[i] = 1;
            a[cur] = i;
            dfs(cur+1);
            visit[i] = 0;
        }
    }
}
int main()
{
    while(scanf("%d%d%d",&one,&two,&three))
    {
        sum = one + two + three;
        if(sum == 0)
            break;
        if(three > one + two)//第三个数的位数大于前2个数的位数值和,直接减掉
        {
            printf("0\n");
            continue;
        }
        if(three < one || three < two)//小于其中一个因子的位数,直接减掉
        {
            printf("0\n");
            continue;
        }
        if(one == 0 || two == 0 || three == 0)//有一个数的位数为0,直接减掉
        {
            printf("0\n");
            continue;
        }
        total = 0;
        memset(visit,0,sizeof(visit));
        dfs(0);
        printf("%d\n",total);
    }
    return 0;
}


HDU 3419 The Three Groups(回溯+减枝)

标签:

原文地址:http://blog.csdn.net/qq_25605637/article/details/51346764

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!