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干货分享】QFix探索之路—手Q热补丁轻量级方案

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57ff5832bb8fec206ce2185d 导语 QFix 是手Q团队近期推 ...

  2. 使用Async和Await进行异步编程(C#版 适用于VS2015)

    你可以使用异步编程来避免你的应用程序的性能瓶颈并且加强总体的响应.然而,用传统的技术来写异步应用是复杂的,同时编写,调试和维护都很困难. VS2012介绍了简单的方法,那就是异步编程,它在.Net F ...

  3. JavaScript练习之for循环语句

    for循环四要素:初始条件.循环条件.循环体.状态改变. 1.for(var a=i;i<=aa;i++) { 循环体(例sum=sum+i  sum是输出的) } 例题 1-20关没关一分 2 ...

  4. Node.js~sails.js~package.json的作用

    回到目录 我们在sails框架进行node.js开发时,会涉及到项目的迁移,当迁移后可能你的module即丢失,这时,希望快速的安装所有的包包,可以使用下面命令 1 cd 你当前的sails项目 2 ...

  5. Atitit 破解qq空间(2)-------探测权限

    Atitit 破解qq空间(2)-------探测权限 /AtiPlatf_cms/src/com/attilax/net/httpTest.java  package com.attilax.net ...

  6. 无服务端纯前台导出数据到Excel-JSExcelXML.js 使用指南

    JSExcelXML 使用指南 先来个效果预览 a.前端显示 b.导出excel效果 表头部分 表尾部分 1.功能描述 JsExcelXml 采用js生成excel中可显示的xml格式文本,将输出文本 ...

  7. angular的$resource factory都有啥

    angular的$resource factory都有啥 A factory which creates a resource object that lets you interact with R ...

  8. Python数据类型之“文本序列(Text Sequence)”

    Python中的文本序列类型 Python中的文本数据由str对象或字符串进行处理. 1.字符串 字符串是Unicode码值的不可变序列.字符串字面量有多种形式: 单引号:'允许嵌入"双&q ...

  9. MySQL(二) 数据库数据类型详解

    序言 今天去健身了,感觉把身体练好还是不错的,闲话不多说,把这个数据库所遇到的数据类型今天统统在这里讲清楚了,以后在看到什么数据类型,咱度应该认识,对我来说,最不熟悉的应该就是时间类型这块了.但是通过 ...

  10. gridview里日期显示格式

    Text='<%#Bind("EndDate","{0:yyyy-MM-dd}") %>'