暴力枚举 + 24点 --- hnu : Cracking the Safe

Cracking the Safe
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 46, Accepted users: 12
Problem 12886 : No special judgement
Problem description

Secret agent Roger is trying to crack a safe containing evil Syrian chemical weapons. In order to crack the safe, Roger needs to insert a key into the safe. The key consists of four digits. Roger has received a list of possible keys from his informants that he needs to try out. Trying out the whole list will take too long, so Roger needs to find a way to reduce the list.
A valid key satisfies
a certain condition, which we call the 24 condition. Four digits that satisfy
the 24 condition can be manipulated using addition, subtraction, multiplication,
division and parentheses, in such a way, that the end result equals 24.
For
example, the key (4; 7; 8; 8) satisfies the 24 condition, because (7-8/8)×4 =
24. The key (1; 1; 2; 4) does not satisfy the 24 condition, nor does (1; 1; 1;
1). These keys cannot possibly be the valid key and do not need to be
tried.
Write a program that takes the list of possible keys and outputs for
each key whether it satisfies the 24 condition or not.

Input

On the first line one positive number: the number of test cases, at most 100.
After that per test case:
one line with four space-separated integers a; b;
c; d (1<=a; b; c; d<=9): a possible key.

Output

Per test case:
one line with either “YES” or “NO”, indicating whether
the key satisfies the 24 condition or not.

Sample Input
4
4 7 8 8
1 1 2 4
1 1 1 1
1 3 4 6
Sample Output
YES
NO
NO
YES
Problem Source
BAPC preliminary 2013


Mean:

给你4个数,你需要判断这4个数是否能够通过"+"、"-"、"*"、"/"四种得到24。

analyse:

数据这么小,直接暴力枚举。

先枚举四个数的位置,再枚举三个运算符,最后枚举运算顺序。

解法二:递归求解。

Time complexity:4*4*4*4*4*4*4*5=81920,不超过81920次

Source code:

//Memory   Time
// 1143K   27MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL long long
using namespace std;
bool mark;
double num[5];
double aa[5];
double calc(double a,double b,int flag)
{
    switch(flag)
    {
        case 1:return (a+b);
        case 2:return (a-b);
        case 3:return (a*b);
        case 4:return (a/b);
    }
}
void algebra()
{
    double tmp1,tmp2,tmp3;
    for(int i=1;i<=4;i++)
    {
        for(int j=1;j<=4;j++)
        {
            for(int k=1;k<=4;k++)
            {
                //  运算顺序1
                tmp1=calc(aa[1],aa[2],i);
                tmp2=calc(tmp1,aa[3],j);
                tmp3=calc(tmp2,aa[4],k);
                if(fabs(tmp3-24)<1e-6)
                {
                   mark=1;
                    return ;
                }
                //  运算顺序2
                tmp1=calc(aa[1],aa[2],i);
                tmp2=calc(aa[3],aa[4],k);
                tmp3=calc(tmp1,tmp2,j);
                if(fabs(tmp3-24)<1e-6)
                {
                   mark=1;
                    return ;
                }
                //  运算顺序3
                tmp1=calc(aa[2],aa[3],j);
                tmp2=calc(aa[1],tmp1,i);
                tmp3=calc(tmp2,aa[4],k);
                if(fabs(tmp3-24)<1e-6)
                {
                   mark=1;
                    return ;
                }
                //  运算顺序4
                tmp1=calc(aa[2],aa[3],j);
                tmp2=calc(tmp1,aa[4],k);
                tmp3=calc(tmp2,aa[1],i);
                if(fabs(tmp3-24)<1e-6)
                {
                   mark=1;
                    return ;
                }
                //  运算顺序5
                tmp1=calc(aa[3],aa[4],k);
                tmp2=calc(aa[2],tmp1,j);
                tmp3=calc(aa[1],tmp2,i);
                if(fabs(tmp3-24)<1e-6)
                {
                   mark=1;
                    return ;
                }
            }
        }
    }
}

void arrange()
{
    for(int i=1;i<=4;i++)
    {
        for(int j=1;j<=4;j++)
        {
            if(j==i)continue;
            for(int k=1;k<=4;k++)
            {
                if(k==i||k==j)continue;
                for(int l=1;l<=4;l++)
                {
                    if(l==i||l==j||l==k)continue;
                    aa[1]=num[i],aa[2]=num[j],aa[3]=num[k],aa[4]=num[l];
                    algebra();
                }
            }
        }
    }
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        mark=false;
        for(int i=1;i<=4;i++)
            cin>>num[i];
        arrange();
        if(mark)
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}

  递归:

//Memory   Time
// 724K      0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 1100
#define LL long long
using namespace std;
double num[4];
bool solve ( int n ) {
    if ( n == 1 ) {
        if ( fabs ( num[0] - 24 ) < 1E-6 )
            return true;
        else
            return false;
    }

    for ( int i = 0; i < n; i++ )
    {
        for ( int j = i + 1; j < n; j++ )
        {
                    double a, b;
                    a = num[i];
                    b = num[j];
                    num[j] = num[n - 1];

                    num[i] = a + b;
                    if ( solve ( n - 1 ) )
                        return true;

                    num[i] = a - b;
                    if ( solve ( n - 1 ) )
                        return true;

                    num[i] = b - a;
                    if ( solve ( n - 1 ) )
                        return true;

                    num[i] = a * b;
                    if ( solve ( n - 1 ) )
                        return true;

                    if ( b != 0 ) {
                        num[i] = a / b;

                        if ( solve ( n - 1 ) )
                            return true;
                    }
                    if ( a != 0 ) {
                        num[i] = b / a;
                        if ( solve ( n - 1 ) )
                            return true;
                    }
                    num[i] = a;
                    num[j] = b;
        }
    }
    return false;
}
int main() {
    int x;
    int T;
    cin >> T;
    while ( T-- ) {
        for ( int i = 0; i < 4; i++ ) {
            cin >> x;
            num[i] = x;
        }

        if ( solve ( 4 ) ) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }

    return 0;
}

  

  

暴力枚举 + 24点 --- hnu : Cracking the Safe,布布扣,bubuko.com

时间: 2024-10-07 15:58:05

暴力枚举 + 24点 --- hnu : Cracking the Safe的相关文章

HNU 12886 Cracking the Safe(暴力枚举)

题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数,要你判断用 + .- . * ./.四种运算能不能得到一个结果为24的式子,可以用括号. 解释一下测试的第四组样例:应该是6 / (1 - 3 / 4) 暴力枚举三种符号分别是什么,然后枚举这三种符号运算的顺序,然后枚举这四个数字的24种排列方式,时间是4^3 * 6 * 24 然后注意要用double型,

HNU 12886 Cracking the Safe 二十四点的判断

经典的一个题,今天竟然写跪了…… 题意: 给你4个数字,让你判断是否能通过四则运算和括号,凑成24点. 思路: 暴力枚举运算顺序和运算符. 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algorithm> 7 #include <stri

HDU 4770 Lights Against Dudely 暴力枚举+dfs

又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ czy Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1360    Accepted Subm

HDU-1015 Safecracker(暴力枚举)

题目回顾(HDU-1015) Safecracker Problem Description "The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in World War II. Fortunat

hdu_2328_Corporate Identity(暴力枚举子串+KMP)

题目链接:hdu_2328_Corporate Identity 题意: 给你n个串,让你找这n个串的最大公共子串 题解: 串比较小,暴力枚举第一个的子串,然后KMP判断是否可行 1 #include<cstdio> 2 #include<cstring> 3 #define F(i,a,b) for(int i=a;i<=b;i++) 4 5 const int N=210; 6 int nxt[N],n,lens[4001],ans,l,r,cnt; 7 char dt[

Coderforces 633D:Fibonacci-ish(map+暴力枚举)

http://codeforces.com/problemset/problem/633/D D. Fibonacci-ish Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elements f0 and f1 are arbi

hdu1172猜数字(暴力枚举)

猜数字 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3373    Accepted Submission(s): 1975 Problem Description 猜数字游戏是gameboy最喜欢的游戏之一.游戏的规则是这样的:计算机随机产生一个四位数,然后玩家猜这个四位数是什么.每猜一个数,计算机都会告诉玩家猜对几个数字,其中

hihoCoder #1179 : 永恒游戏 (暴力枚举)

题意:给出一个有n个点的无向图,每个点上有石头数个,现在的游戏规则是,设置某个点A的度数为d,如果A点的石子数大于等于d,则可以从A点给每个邻接点发一个石子.如果游戏可以玩10万次以上,输出INF,否则输出最多能玩几次. 思路:暴力枚举每个可以玩的点,假如可以玩无限次,且当前状态为Z(指所有点的石头数的序列作为一个状态),那么在玩了多次之后,一定会造成循环,也就是说,玩几次之后,每个点的石子数和初始的石子数一模一样,这样子我再重复之前是怎么玩的就可以无限玩了.但是由于有200个点,所以玩一次就去

2014网选ZOJPretty Poem(暴力枚举)

/* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<string> 5 6 using namespace std; 7 string str; 8 char ch[55]; 9 int main(){ 10 int t; 11 scanf("%d&