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
  1. 4
  2. 4 7 8 8
  3. 1 1 2 4
  4. 1 1 1 1
  5. 1 3 4 6
Sample Output
  1. YES
  2. NO
  3. NO
  4. 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. 可在广域网部署运行的QQ高仿版 -- GG叽叽V2.4,增加远程协助、桌面共享功能(源码)

    QQ的远程协助.或者说桌面共享是一个非常实用的功能,所以,2.4版本的GG复制了它,而且,GG增强了桌面共享的功能,它可以允许指定要共享桌面的区域,这样,对方就只能看到指定区域的桌面,这对节省流量会非 ...

  2. 新版markdown功能发布!支持github flavored markdown!

    让大家久等了!新版markdown功能一直拖到今天才发布,很是愧疚...但不管怎么样,总算发布了! 今年1月份发布第一版markdown功能之后,很多园友反馈说做得很烂,我们综合大家的反馈之后发现不仅 ...

  3. Flash 与 php 使用 amfphp

    创建 Flash 项目 使用 Flash Builder 创建一个项目. 创建 Flash 项目时,选择服务器技术为 PHP,并配置好服务器的 Web 根文件夹及根 URL 地址(这里设置根文件夹时, ...

  4. Windows内存小结

    以前写过一篇理解程序内存, 当时主要是针对用户态,下面再稍微深入一点: 我们以32位程序为例(不启用AWE), 总共4G虚拟空间,其中低2G属于用户态, 高2G属于操作系统内核, 每个程序都有自己的低 ...

  5. [异常解决] android studio检测不到手机的解决办法——ADB驱动自己安装

    用android studio搭建安卓开发环境比eclipse简单的多,仅仅下载一个1个G左右的安装包安装即可. 安装好之后随便建一个hello world工程,想用实体手机调试要点: 选择USB设备 ...

  6. iOS UITableView行高自行扩展

    myTableView.estimatedRowHeight = ; myTableView.rowHeight = UITableViewAutomaticDimension; 不需要实现 - (C ...

  7. 新特性AAtitti css3 新特性attilax总结titti css

    Atitti css3 新特性attilax总结 图片发光效果2 透明渐变效果2 文字描边2 背景拉伸2 CSS3 选择器(Selector)4 @Font-face 特性7 Word-wrap &a ...

  8. Atitit.会员卡(包括银行卡)api的设计

    Atitit.会员卡(包括银行卡)api的设计 1. 银行卡的本质是一种商业机构会员卡1 2. 会员卡号结构组成1 2.1. ●前六位是:发行者标识代码 Issuer Identification N ...

  9. react-native —— 在Windows下搭建React Native Android开发环境

    在Windows下搭建React Native Android开发环境 前段时间在开发者头条收藏了 @天地之灵_邓鋆 分享的<在Windows下搭建React Native Android开发环 ...

  10. SpringAOP之静态代理

    一.SpringAOP: ⒈AOP:Aspect Oriented Programming 面向切面编程, 实现的是核心业务和非核心业务之间的的分离,让核心类只做核心业务,代理类只做非核心业务.  ⒉ ...