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[];
double aa[];
double calc(double a,double b,int flag)
{
   switch(flag)
   {
       case :return (a+b);
       case :return (a-b);
       case :return (a*b);
       case :return (a/b);
   }
}
void algebra()
{
   double tmp1,tmp2,tmp3;
   for(int i=;i<=;i++)
   {
       for(int j=;j<=;j++)
       {
           for(int k=;k<=;k++)
           {
               //  运算顺序1
               tmp1=calc(aa[],aa[],i);
               tmp2=calc(tmp1,aa[],j);
               tmp3=calc(tmp2,aa[],k);
               if(fabs(tmp3-)<1e-6)
               {
                  mark=;
                   return ;
               }
               //  运算顺序2
               tmp1=calc(aa[],aa[],i);
               tmp2=calc(aa[],aa[],k);
               tmp3=calc(tmp1,tmp2,j);
               if(fabs(tmp3-)<1e-6)
               {
                  mark=;
                   return ;
               }
               //  运算顺序3
               tmp1=calc(aa[],aa[],j);
               tmp2=calc(aa[],tmp1,i);
               tmp3=calc(tmp2,aa[],k);
               if(fabs(tmp3-)<1e-6)
               {
                  mark=;
                   return ;
               }
               //  运算顺序4
               tmp1=calc(aa[],aa[],j);
               tmp2=calc(tmp1,aa[],k);
               tmp3=calc(tmp2,aa[],i);
               if(fabs(tmp3-)<1e-6)
               {
                  mark=;
                   return ;
               }
               //  运算顺序5
               tmp1=calc(aa[],aa[],k);
               tmp2=calc(aa[],tmp1,j);
               tmp3=calc(aa[],tmp2,i);
               if(fabs(tmp3-)<1e-6)
               {
                  mark=;
                   return ;
               }
           }
       }
   }
}

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

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

递归:

//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[];
bool solve ( int n ) {
   if ( n == ) {
       if ( fabs ( num[] - ) < 1E-6 )
           return true;
       else
           return false;
   }

for ( int i = ; i < n; i++ )
   {
       for ( int j = i + ; j < n; j++ )
       {
                   double a, b;
                   a = num[i];
                   b = num[j];
                   num[j] = num[n - ];
                   
                   num[i] = a + b;
                   if ( solve ( n - ) )
                       return true;
                       
                   num[i] = a - b;
                   if ( solve ( n - ) )
                       return true;

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

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

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

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

return ;
}

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

  1. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

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

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

  3. HDU 4770 Lights Against Dudely 暴力枚举+dfs

    又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...

  4. HDU 1015.Safecracker【暴力枚举】【8月17】

    Safecracker Problem Description === Op tech briefing, 2002/11/02 06:42 CST ===  "The item is lo ...

  5. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  6. 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)

    /* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...

  7. 51nod 1116 K进制下的大数 (暴力枚举)

    题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...

  8. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  9. bzoj 1028 暴力枚举判断

    昨天梦到这道题了,所以一定要A掉(其实梦到了3道,有两道记不清了) 暴力枚举等的是哪张牌,将是哪张牌,然后贪心的判断就行了. 对于一个状态判断是否为胡牌,1-n扫一遍,然后对于每个牌,先mod 3, ...

随机推荐

  1. ENode 1.0 - 事件驱动架构(EDA)思想的在框架中如何体现

    开源地址:https://github.com/tangxuehua/enode 上一篇文章,我给大家分享了我的一个基于DDD以及EDA架构的框架enode,但是只是介绍了一个大概.接下来我准备用很多 ...

  2. java 多线程(ThreadPoolExecutor (补充))

    import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; impo ...

  3. 《C#图解教程》读书笔记之二:存储、类型和变量

    本篇已收录至<C#图解教程>读书笔记目录贴,点击访问该目录可获取更多内容. 一.类型初窥:掀起你的盖头来 (1)C程序是一组函数和数据类型,C++程序是一组函数和类,而C#程序是一组类型声 ...

  4. Beauty Contest

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28417#problem/F 题目大意:给n个点,求相聚最远距离的平方(输出整形) 集体思 ...

  5. SWF READER 破解日志。

    网上传闻swf reader是破解最厉害的神器,可以内存抓取+doSWF反编译.所以去官网下了一个: SWF_Reader_2.3 不出所料,demo版本没有反编译的功能.网上搜到一个哥们尝试了下: ...

  6. CGLib与JDKProxy的区别

    Spring AOP 的实现主要有两种:CGLib与JDK自带的Proxy. 他们主要的区别是,需要JDKProxy修改的类必须实现接口(因此也只能代理public方法),在创建Proxy时可以使用c ...

  7. Java-继承,多态0922-05

    28.按要求编写一个Java应用程序: (1)定义一个类,描述一个矩形,包含有长.宽两种属性,和计算面积方法. (2)编写一个类,继承自矩形类,同时该类描述长方体,具有长.宽.高属性, 和计算体积的方 ...

  8. Java-数组练习2

    2.找出如下数组中最大的元素和最小的元素,a[][]={{3,2,6},{6,8,2,10},{5},{12,3,23}} int[][] i={{3,2,6},{6,8,2,10},{5},{12, ...

  9. Laravel5.0学习--02 实例进阶

    本文以laravel5.0.22为例. 本节以新建一个简单的博客作为实例. 准备工作 数据库配置 .env文件(也可以直接修改config/database.php) DB_HOST=localhos ...

  10. Atitit 理解Monad attilax总结

    Atitit 理解Monad attilax总结 但函数式编程最大的一个问题是,函数是一个数学抽象,在现实世界中不存在,1 那既然这样就够用了,还要 Monad 干嘛?Monad 的作用在这里就体现出 ...