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. 【腾讯Bugly干货分享】浅谈Android自定义锁屏页的发车姿势

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57875330c9da73584b025873 一.为什么需要自定义锁屏页 锁屏 ...

  2. 【C语言学习】《C Primer Plus》第8章 字符输入/输出和输入确认

    学习总结 1.缓冲区分为完全缓冲区(fully buffered)I/O和行缓冲区(line-buffered)I/O.对完全缓冲输入来说,当缓冲区满的时候会被清空(缓冲区内容发送至其目的地).这类型 ...

  3. 跟我一起云计算(1)——storm

    概述 最近要做一个实时分析的项目,所以需要深入一下storm. 为什么storm 综合下来,有以下几点: 1. 生逢其时 MapReduce 计算模型打开了分布式计算的另一扇大门,极大的降低了实现分布 ...

  4. Bootstrap~多级导航(级联导航)的实现

    回到目录 在bootstrap官方来说,导航最多就是两级,两级以上是无法实现的,大叔找了一些第三方的资料,终于找到一个不错的插件,使用上和效果上都还不错,现在和大家分享一下 插件地址:http://v ...

  5. 爱上MVC3~为下拉列表框添加一个自定义验证规则

    回到目录 开发它的原因: 之前的同事,也是我的哥们,问我下拉列表框是否可以支持验证,这个问题看似简单,但确实MVC里有为我们提供,所以,只能自己写个扩展了,即自己写一个attribute特性,让它继承 ...

  6. Java程序员的日常 —— 多进程开发IO阻塞问题

    本篇仍旧是源于最近的工作,总结一下纪念那些年埋下的坑... 背景故事 需求:"使用进程方式启动另一个程序!" 开发:"OK! Runtime.getRuntime().e ...

  7. Android笔记——了解SDK,数据库sqlite的使用

    一.adb是什么? adb的全称为Android Debug Bridge,就是起到调试桥的作用.通过adb我们可以在Eclipse中方面通过DDMS来调试Android程序,说白了就是debug工具 ...

  8. settimeout里面函数有无双引号的区别

    在写定时器时很容易搞混,所以记下防止忘记. 双引号中的作用域不捕捉局部变量,不用双引号包着的是捕捉局部作用域 var a = function() { alert(1111) } function a ...

  9. 【WP 8.1开发】如何处理摄像头翻转的问题

    模拟器就像我们儿时的梦境,在其上运行应用程序时,一切总是那么美好的:而真机测试如同我们这个纷乱无章的现实世界,你会遇到各种小人和畜生,常常会遭受莫名的挫折.面对挫折,有人迎难而上,或不予理采,走自己的 ...

  10. 深入理解PHP内核(十三)类的结构和实现

    原文链接:http://www.orlion.ga/1117/ 先看一下类的结构: struct _zend_class_entry {     char type;     // „类型:ZEND_ ...