速算24点

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Problem 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
 
Author
LL
 思路:暴力搜索,利用next_permutation全排列
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
#define esp 0.00000000001
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
int a[];
int ans;
int getnum(string a)
{
if(a[]=='')
return ;
if(a[]>=''&&a[]<='')
return a[]-'';
if(a[]=='A')
return ;
if(a[]=='J')
return ;
if(a[]=='K')
return ;
if(a[]=='Q')
return ;
}
void dfs(int num,int gg,int step)
{
if(step==)
{
if(num==)
ans=;
return;
}
//不加括号
dfs(num+gg,a[step+],step+);
dfs(num-gg,a[step+],step+);
dfs(num*gg,a[step+],step+);
if(gg!=&&num%gg==)
dfs(num/gg,a[step+],step+);
//加括号
if(step!=)
{
dfs(num,gg+a[step+],step+);
dfs(num,gg*a[step+],step+);
dfs(num,gg-a[step+],step+);
if(a[step+]!=&&gg%a[step+]==)
dfs(num,gg/a[step+],step+);
}
}
string ch[];
int main()
{
int x,y,z,i,t;
while(cin>>ch[]>>ch[]>>ch[]>>ch[])
{
ans=;
for(i=;i<;i++)
a[i]=getnum(ch[i]);
sort(a,a+);
do
{
dfs(a[],a[],);
}
while(next_permutation(a,a+));
if(ans)
printf("Yes\n");
else
printf("No\n");
}
return ;
}

hdu 1427 速算24点 dfs暴力搜索的更多相关文章

  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暴力搜索)

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

  3. hdu 1427 速算24点

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1427 速算24点 Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A( ...

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

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

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

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

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

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

  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. hdu1427之速算24点

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

  9. ACM: Gym 100935G Board Game - DFS暴力搜索

    Board Game Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u  Gym 100 ...

随机推荐

  1. 配置tomcat通过客户端访问

    1:在tomcat    conf/tomcat-users.xml 文件里  配置用户名和密码,以及访问方式 For example, to add the manager-gui role to ...

  2. JSP表单提交与接收

    JSP表单提交与接收 在Myeclipse中新建web project,在webroot中新建userRegist1.jsp,代码如下 <%@ page contentType="te ...

  3. jenkins 项目发布脚本

    构建shell #!/bin/bash ########################################################################## 编译部分 ...

  4. [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree_Medium tag: Preorder Traversal, tree

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  5. 使用sys用户创建其他用户下的dblink

    因为dblink的创建和删除只能是它的所属用户来操作,所以我们无法直接使用sys用户创建其他用户下的dblink,当遇到有这样的需求时,可以先建立该用户下存储过程,再通过调用这个存储过程来间接实现. ...

  6. springmvc学习笔记一框架的理解

    SpringMVC现在在很多公司都很流行,所以这个框架对我们来说,是很重要的. 首先我们对比mvc来分析springmvc这个框架是怎么设计,以及它的工作的流程. 首先来看mvc: 1.  用户发起r ...

  7. HashMap(JDK1.9)详解

    一.HashMap的概念. 1.HashMap类的继承实现关系如下:因此HashMap的功能有:可序列化.可克隆等功能. 2.HashMap的数据结构:数组+链表+红黑树. 3.键值对的存储方案:第一 ...

  8. codeforces D - Arthur and Walls

    这题说的是给了一个矩阵,必须让.连接起来的图形变成矩形,2000*2000的矩形,那么我们就可以知道了,只要是存在一个有点的区域应该尽量将他削为矩形,那么将这个图形进行缩放,最后我们知道只要存在一个2 ...

  9. Geometry

    uva1473 这题说的是 在空间中给了n个点 然后用体积最小的圆锥将这些点包含在内可以在表面上, 将这些点 映射到xoz平面上然后,然后枚举每个上凸包的边和每个点的极值进行判断求得最小的体积 我们会 ...

  10. Python: 合并多个字典

    现在有多个字典或者映射,想将它们从逻辑上合并为一个单一的映射后执行某些操作,比如查找值或者检查某些键是否存在. eg1: a = {'x': 1, 'z': 3 }b = {'y': 2, 'z': ...