题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1427

速算24点

Description

速算24点相信绝大多数人都玩过。就是随机给你四张牌,包括A(1),2,3,4,5,6,7,8,9,10,J(11),Q(12),K(13)。要求只用'+','-','*','/'运算符以及括号改变运算顺序,使得最终运算结果为24(每个数必须且仅能用一次)。游戏很简单,但遇到无解的情况往往让人很郁闷。你的任务就是针对每一组随机产生的四张牌,判断是否有解。我们另外规定,整个计算过程中都不能出现小数。

Input

每组输入数据占一行,给定四张牌。

Output

每一组输入数据对应一行输出。如果有解则输出"Yes",无解则输出"No"。

Sample Input

A 2 3 6
3 3 8 8

Sample Output

Yes
No

参照<<编程之美>>上的写法,时间卡的挺紧的,用cin tle scanf过了/(ㄒoㄒ)/~~

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<map>
using std::map;
using std::fabs;
using std::pair;
using std::swap;
using std::string;
using std::next_permutation;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 20;
const double eps = 1E-7;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
int arr[4];
char ret[4][10];
map<string, int> A;
bool dfs(int n) {
if (1 == n) {
return arr[0] == 24;
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int a = arr[i], b = arr[j];
arr[j] = arr[n - 1];
arr[i] = a + b;
if (dfs(n - 1)) return true;
arr[i] = a - b;
if (dfs(n - 1)) return true;
arr[i] = b - a;
if (dfs(n - 1)) return true;
arr[i] = a * b;
if (dfs(n - 1)) return true;
if (b != 0 && 0 == a % b) {
arr[i] = a / b;
if (dfs(n - 1)) return true;
}
if (a != 0 && 0 == b % a) {
arr[i] = b / a;
if (dfs(n - 1)) return true;
}
arr[i] = a;
arr[j] = b;
}
}
return false;
}
void init() {
A["A"] = 1;
A["2"] = 2;
A["3"] = 3;
A["4"] = 4;
A["5"] = 5;
A["6"] = 6;
A["7"] = 7;
A["8"] = 8;
A["9"] = 9;
A["10"] = 10;
A["J"] = 11;
A["Q"] = 12;
A["K"] = 13;
}
bool solve() {
for (int i = 0; i < 4; i++) {
arr[i] = A[ret[i]];
}
do {
if (dfs(4)) return true;
} while (next_permutation(arr, arr + 4));
return false;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
init();
while (~scanf("%s %s %s %s", ret[0], ret[1], ret[2], ret[3])) {
puts(solve() ? "Yes" : "No");
}
return 0;
}

hdu 1427 速算24点的更多相关文章

  1. hdu 1427 速算24点【暴力枚举】

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  2. hdu 1427 速算24点 dfs暴力搜索

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem De ...

  3. HDU 1427 速算24点 (深搜)

    题目链接 Problem Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A(1),2,3,4,5,6,7,8,9,10,J(11),Q(12),K(13).要求只用' ...

  4. HDU 1427 速算24点【数值型DFS】

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. 24点游戏&&速算24点(dfs)

    24点游戏 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...

  6. hdu1427之速算24点

    速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  7. Hdu1427 速算24点 2017-01-18 17:26 46人阅读 评论(0) 收藏

    速算24点 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  8. HDOJ 1427(dfs) 速算24点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路分析: 题目要求判断是否存在一种运算组合使得4个数的计算结果为24,因为搜索的层次为3层,不 ...

  9. [HDU 1427]速度计算24点(DFS暴力搜索)

    主题连接:  pid=1427">http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路:简单的DFS.dfs(sum,next,p)表 ...

随机推荐

  1. 关于NopCommerce3.6版用户登录详解

    一.登录方式 Nop登录方式有两种(且只能选择一种方式登录):一种是用用户名登录,另一种是用户注册邮箱登录,这个在后台可配置: 第一种:用户名登录 后台配置路径在商城设置à设置管理à客户设置:使用用户 ...

  2. 学习总结 java连接数据库

    package com.hanqi.test; import java.sql.*; public class jdbcTest { public static void main(String[] ...

  3. .NET本质论(4)应用程序对象HttpApplication

    当HttpContext对象创建之后,HttpRuntime将随后创建一个用于处理请求的对象,这个对象的类型为HttpApplication.  在ASP.NET内部,HttpRuntime管理一个定 ...

  4. 设置Eclipse自动跳转到debug模式的小技巧

    默认情况下,eclipse中右键debug,当运行到设置的断点时会自动跳到debug模式下.但由于我的eclipse环境,从开始一直用到现在,中间包括装.卸各种插件,更换版本,从英文界面导到中文界面又 ...

  5. 讲解DLL内容的比较详细的站点

    1.通过 Visual Studio 2008 用C语言创建和调用DLL : http://blog.chinaunix.net/uid-631975-id-116622.html 2.DLL(Dyn ...

  6. CentOS yum kvm

    首先,需要我们的cpu支持虚拟化,有的机器支持但是并未在bios开启,这个需要事先开启. 1. Dell R710安装centos6.7 64位 ,Dell R710在开机后按F2进入BIOS,Pro ...

  7. sqlmap.config 配置

    <?xml version="1.0" encoding="utf-8"?> <sqlMapConfig xmlns="http:/ ...

  8. [Cocos2d-x for WP8学习笔记] HelloWorld结构分析

    先来看一下目录结构: Assets:游戏资源文件,图片音频等,Resource文件夹也有类似功能 include:用于放置游戏头文件 Shaders:渲染器着色器文件(大雾) cocos2dorig. ...

  9. 基于opencv网络摄像头在ubuntu下的视频获取

     基于opencv网络摄像头在ubuntu下的视频获取 1  工具 原料 平台 :UBUNTU12.04 安装库  Opencv-2.3 2  安装编译运行步骤 安装编译opencv-2.3  参 ...

  10. Ubuntu点滴--apt-get update和upgrade的作用

    update update is used to resynchronize the package index files from their sources. The indexes of av ...