hdu 1427 速算24点
题目连接
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点的更多相关文章
- hdu 1427 速算24点【暴力枚举】
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- hdu 1427 速算24点 dfs暴力搜索
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem De ...
- HDU 1427 速算24点 (深搜)
题目链接 Problem Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A(1),2,3,4,5,6,7,8,9,10,J(11),Q(12),K(13).要求只用' ...
- HDU 1427 速算24点【数值型DFS】
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- 24点游戏&&速算24点(dfs)
24点游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...
- hdu1427之速算24点
速算24点 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- Hdu1427 速算24点 2017-01-18 17:26 46人阅读 评论(0) 收藏
速算24点 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submiss ...
- HDOJ 1427(dfs) 速算24点
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路分析: 题目要求判断是否存在一种运算组合使得4个数的计算结果为24,因为搜索的层次为3层,不 ...
- [HDU 1427]速度计算24点(DFS暴力搜索)
主题连接: pid=1427">http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路:简单的DFS.dfs(sum,next,p)表 ...
随机推荐
- Nodejs文件服务器
最近一直在忙于一个比较大的项目,在项目中需要有个文件服务器来支持.老鸟们建议我去用NodeJs来实现,我在接手这个项目之前其实并不了解NodeJs,但是一直想去了解.借着这个机会好好去学习一下.下面是 ...
- 详细讲解JAVA中的IO流
一.流的概念 流(stream)的概念源于UNIX中管道(pipe)的概念.在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备.外部文件等. ...
- 【python】os模块
1.os.name 输出字符串指示正在使用的平台.如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix'. 2.os.getcwd() 函数得到当前工作目录,即当前Py ...
- c++ builder TListView控件按字符串排序(根据网上代码亲测ok)
//--------------------------------------------------------------------------- /* 首先将一个列表框控件安放在Form上, ...
- zookeeper学习记录
ZooKeeper:是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.他主要用来解决分布式应用中的数据管理的一致性问题 ...
- C++ 三种工厂模式
工厂模式是将带有继承于基类的子类的创建过程交于一个工厂来创建,通过赋予不同的创建标识来创建不同的子类. 基于自己的理解和使用这里巩固一下工厂模式. 我们的项目目前使用最多的是简单工厂模式,不过其他两种 ...
- Aspose.cell.dll的使用,导excel表
using System; using System.Web; using EF; using Newtonsoft.Json; using System.Collections.Generic; u ...
- node中的流程控制中,co,thunkify为什么return callback()可以做到流程控制?
前言 我在学习generator ,yield ,co,thunkify的时候,有许多费解的地方,经过了许多的实践,也慢慢学会用,慢慢的理解,前一阵子有个其他项目的同事过来我们项目组学习node,发现 ...
- Windows 2003 FastCgi安装环境
Windows 2003 IIS+PHP5.4.3 安装教程 一.准备相关组件 安装前,先安装IIS. 1.安装FastCgi for IIS6 Fastcgi官方网址是:http://www.iis ...
- Conditional Counting In SQL
If you ever want to conditionally count the number of times a particular condition occurs in SQL, ...