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. 文件系统权限引起IIS站点总跳登录页面

    今天在IIS上部署一个ASP.NET站点时遇到一个很奇怪的问题,不管访问什么页面,都会跳到登录页面,即使是访问静态文件. 折腾半天,百思不得其解,百整不得其果... 后来突然想到,是不是站点所在文件夹 ...

  2. jquery插件——点击交换元素位置(带动画效果)

    一.需求的诞生 在我们的网页或者web应用中,想要对列表中的元素进行位置调整(或者说排序)是一个常见的需求.实现方式大概就以下两种,一种是带有类似“上移”.“下移”的按钮,点击可与相邻元素交换位置,另 ...

  3. C# 获取当前日期在指定日期范围内是第几周

    public static int GetWeekOfDay(DateTime start, DateTime end) { //总周数 )); //用于存储日期 var weekDic = new ...

  4. 在cocos2dx和unity3d之间选择

    人生最纠结的事,莫过于有选择………… cocos2dx和unity3d从某种意义上讲,都很不错.但当面对特定需求以及团队情况的时候,总是能分出高下的. 假设,目标游戏是一款类似 刀塔传奇 的游戏 我们 ...

  5. Stealth视频教程学习笔记(第一章)

    Stealth视频教程学习笔记(第一章) 本文是对Unity官方视频教程Stealth的学习笔记.在此之前,本人整理了Stealth视频的英文字幕,并放到了优酷上.本文将分别对各个视频进行学习总结,提 ...

  6. git 修改管理

    查看修改: 撤销某一文件的修改(还没提交): 撤销所有文件的修改: git checkout .

  7. 关于js内部运行机制的一本好书

    读<单页Web应用一书>,第二章讲了js内部运行机制,感觉棒极了.之前读<你不知道的js>,看的云里雾里,似懂非懂.没想到单页Web一书将此内容讲的如此通俗易懂,好多困惑已久的 ...

  8. 为什么Java方法里面不能再嵌套方法?

    直接原因: 这是Java基本语法定义的,方法中不可以再次声明方法,只能调用其他的方法. 个人理解: 1.方法栈是需要一个载体的,这个载体就是Class,如果一个方法的上一级不是一个类,就说明没有载体. ...

  9. NoSQL和Redis简介及Redis在Windows下的安装和使用教程

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/103.html?1455869099 NoSQL简介 介绍redis前,我 ...

  10. MyBatis学习总结(三)——优化MyBatis配置文件中的配置

    一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: 1 <?xml version=" ...