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

题目地址: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;
}
时间: 2024-07-30 16:13:56

HDU 3419 The Three Groups(回溯+减枝)的相关文章

hdu4770:Lights Against Dudely(回溯 + 减枝)

题目:hdu4770:Lights Against Dudely 题目大意:同样是n*m的矩阵代表room,房间同样也有脆弱和坚固之分,现在要求要保护脆弱的房间,需要将每个脆弱的房间都照亮,但是坚固的房间不允许照到灯.灯是成L形的,即在x,y上有一盏灯,那么(x - 1, y)和(x, y + 1)就可以被照亮,当然包括(x,y).题目又提供了一盏特殊的灯,它可以该改变方向,但是只有一盏,可以用也可以不用.一个房间最多一盏灯.问要将这些脆弱的房间都照亮最少需要多少盏灯. 解题思路:题目有说脆弱的

HDU 5113 Black And White(暴力dfs+减枝)

题目大意:给你一个n×m的矩阵,然后给你k种颜色,每种颜色有x种,所有的个数加起来恰好为n×m个.问你让你对这个矩阵进行染色问你,能不能把所有的小方格都染色,而且相邻两个颜色不同. 思路:一开始想的是构造,先按照个数进行排序,枚举每一个位置,贪心的策略先放多的,如果可以全部放下就输出YES,以及存贮的方案,否则输出NO,但是有bug,一直不对... 正解:dfs暴力枚举每一个点,裸的话需要25!,显然会超时,需要先排个序用构造的策略,让多的先放这样可以减枝.然后再dfs就可以了. Black A

bzoj 2241: [SDOI2011]打地鼠(暴搜+减枝)

2241: [SDOI2011]打地鼠 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 1098  Solved: 691 [Submit][Status][Discuss] Description 打地鼠是这样的一个游戏:地面上有一些地鼠洞,地鼠们会不时从洞里探出头来很短时间后又缩回洞中.玩家的目标是在地鼠伸出头时,用锤子砸其头部,砸到的地鼠越多分数也就越高. 游戏中的锤子每次只能打一只地鼠,如果多只地鼠同时探出头,玩家只能通过多次挥舞锤子的方

【UVA】1232 - SKYLINE(线段树减枝)

注意中间的减枝,还需要用一个tr[i]记录结点的值,用col[i]记录结点区间是否被全覆盖. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 111111; const int maxd = 100001; #define lson pos<<1 #define rson pos<<1|1 int col[ma

2014鞍山现场赛H题HDU5077(DFS减枝+打表)

NAND Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 65    Accepted Submission(s): 14 Problem Description Xiaoqiang entered the "shortest code" challenge organized by some self-claimed a

hdu 4770 Lights Against Dudely(回溯)

题目链接:hdu 4770 Lights Against Dudely 题目大意:在一个N*M的银行里,有N*M个房间,'#'代表坚固的房间,'.'代表的是脆弱的房间,脆弱的房间个数不会超过15个,现在为了确保安全,要在若干个脆弱的房间上装灯,普通的灯是照亮{0, 0}, {-1, 0}, {0, 1}(和题目中坐标有点出入),然后可以装一个特殊的,可以照射 { {0, 0}, {0, 1}, {1, 0} }, { {0, 0}, {-1, 0}, {0, -1} }, { {0, 0}, {

dfs减枝回溯! HDU 5113

#include<bits/stdc++.h> using namespace std; int n,m,k; int yy[60]; int g[30][30]; bool jud(int x,int y,int i){ if(x<0||x>=n||y<0||y>=m) return true; if(g[x][y]!=i) return true; return false; } bool Cut(int step) { int le = (n*m-step+1)/

HDU 1010 深搜+减枝

HDU 1010 /************************************************************************* > File Name: HDU1010.cpp > Author:yuan > Mail: > Created Time: 2014年11月05日 星期三 22时22分56秒 **********************************************************************

[HDU 1016]--Prime Ring Problem(回溯)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2