题目

题目链接

题意

给出4个数字,让你任意指定运算符(3个)、增加括号、交换数,问组成24点的代价最小是多少。

增加括号:代价为1。

交换数的顺序:代价为2。

题解

方法就是暴力枚举,我们可以先枚举数的顺序(4!4!4!种可能性),再枚举运算符类型(43434^3种可能性),再枚举表达式树的结构(555种情况,计算可以使用卡特兰数计算)。
然后在把枚举的数的顺序、运算符填入表达式树中去。

至于填括号的操作,我们在一颗完整的表达式树构造完成后,可以判断∗、/" role="presentation" style="position: relative;">∗、/∗、/* 、/节点下是否有直接有+、−+、−+、-,有几个+、−+、−+、-就要增加几对括号。

这样的话费用就计算出来了,下面只需要按照后序遍历计算表达式树,判断结果是否为24就可以了。

注意如果不能整除的话,要提前终止计算,返回当前情况不满足。

在我写这道题的时候,排列、排列的代价、表达式树的结构种类我都手动计算出来了,所以在这道题里面,显得比较暴力。

代码

#include <iostream>
#include <algorithm>
using namespace std;
int a[4],na[4];
int ps[][4] = {{0,1,2,3},{0,1,3,2},{0,2,1,3},{0,2,3,1},{0,3,1,2},{0,3,2,1},{1,0,2,3},{1,0,3,2},{1,2,0,3},{1,2,3,0},{1,3,0,2},{1,3,2,0},{2,0,1,3},{2,0,3,1},{2,1,0,3},{2,1,3,0},{2,3,0,1},{2,3,1,0},{3,0,1,2},{3,0,2,1},{3,1,0,2},{3,1,2,0},{3,2,0,1},{3,2,1,0}};
int cp[] = {0,1,1,2,2,3,1,2,2,3,3,4,2,3,3,4,4,5,3,4,4,5,5,6};
struct node{int num,lson,rson;
}ns[100];
void init(){ns[1].lson=2;ns[1].rson=3;ns[2].lson=4;ns[2].rson=5;ns[3].lson=6;ns[3].rson=7;ns[8].lson=9;ns[8].rson=10;ns[9].lson=11;ns[9].rson=12;ns[11].lson=13;ns[11].rson=14;ns[15].lson=16;ns[15].rson=17;ns[16].lson=18;ns[16].rson=19;ns[19].lson=20;ns[19].rson=21;ns[22].lson=23;ns[22].rson=24;ns[24].lson=25;ns[24].rson=26;ns[25].lson=27;ns[25].rson=28;ns[29].lson=30;ns[29].rson=31;ns[31].lson=32;ns[31].rson=33;ns[33].lson=34;ns[33].rson=35;
}
void tree_fill(int rt,int i,int& idx,int& op){if(ns[rt].lson == 0 && ns[rt].rson == 0){ns[rt].num = a[ps[i][idx++]];return ;}tree_fill(ns[rt].lson,i,idx,op);ns[rt].num = -(op%4)*1000 - 1000;op /= 4;tree_fill(ns[rt].rson,i,idx,op);
}
int calc(int rt,int &cost){if(ns[rt].lson == 0 && ns[rt].rson == 0) return ns[rt].num;if(ns[rt].num == -3000 || ns[rt].num == -4000){if(ns[ns[rt].lson].num == -1000 || ns[ns[rt].lson].num == -2000) cost ++;if(ns[ns[rt].rson].num == -1000 || ns[ns[rt].rson].num == -2000) cost ++;}int l = calc(ns[rt].lson,cost);int r = calc(ns[rt].rson,cost);if(l == -1000 || r == -1000) return -1000;switch(ns[rt].num){case -1000:return l+r;case -2000:return l-r;case -3000:return l*r;case -4000:if(r == 0 || l % r != 0) return -1000;return l / r;}
}
int mi = 1000;
int main(){init();cin>>a[0]>>a[1]>>a[2]>>a[3];for(int t = 0;t < 5;++t)for(int i = 0;i < 24;++i){for(int j = 0;j < 64;++j){int idx = 0,op = j,cost = 2*cp[i];tree_fill(t*7+1,i,idx,op);int res = calc(t*7+1,cost);if(res == 24) mi = min(mi,cost);}}if(mi == 1000) puts("impossible");else printf("%d\n",mi);
}

codeforces gym-101673 Twenty Four, Again 24点,枚举表达式树过题相关推荐

  1. Codeforces Gym 101173 CERC 16 D BZOJ 4790 Dancing Disks

    Codeforces Gym 101173 CERC 16 D & BZOJ 4790 Dancing Disks 强烈安利这道构造题目,非常有意思. 这里用到的思想是归并排序! 多路归并排序 ...

  2. Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven)

    Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven) 题目来源: Codeforces 题意: 给出一些比赛, ...

  3. [Codeforces Gym 101651/100725B] Banal Tickets

    Codeforces Gym 100725 题解: 先分两种情况, 积为000与积非0" role="presentation" style="position ...

  4. Error:(63, 24) 错误: 枚举 switch case 标签必须为枚举常量的非限定名称

    错误描述: Error:(63, 24) 错误: 枚举 switch case 标签必须为枚举常量的非限定名称. 项目当中用到了饼状图,所以用到了开源的图表库,功能相当强大,传送门:XCL_Chart ...

  5. Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或)

    Educational Codeforces Round 23:E. Choosing The Commander(字典树01异或) 题意: 3种操作: 1 插入一个数 2 删除一个数 3 给出一个数 ...

  6. Codeforces Gym 100269 Dwarf Tower (最短路)

    题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...

  7. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  8. Codeforces Gym 101142C:CodeCoder vs TopForces(搜索)

    http://codeforces.com/gym/101142/attachments 题意:每个人在TC和CF上分别有两个排名,如果有一个人在任意一个网站上大于另一个人的排名,那么这个人可以打败另 ...

  9. 【最短路】NEERC15 F Froggy Ford(2015-2016 ACM-ICPC)(Codeforces GYM 100851)

    题目链接: http://codeforces.com/gym/100851 题目大意: 一只青蛙跳过宽为W的河,河中游N个石头,坐标xi,yi,现在往河中间添加一个石头,使得每次跳跃的最大的距离最小 ...

最新文章

  1. Python,OpenCV中的K近邻(knn K-Nearest Neighbor)及改进版的K近邻
  2. 如何使用TensorRT对训练好的PyTorch模型进行加速?
  3. 如何使用SAP零售系统中的LISTING?
  4. Gitlab上传代码
  5. NSArray和NSMutableArray对象的使用
  6. .NET Core中文分词组件jieba.NET Core
  7. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 85
  8. 面向对象——一起来复习托付与事件!
  9. 入门机器学习(九)--应用机器学习的建议
  10. Git 更安全的强制推送,--force-with-lease
  11. 李彦宏候选工程院院士;陌陌回应探探下架;拼多多回应“刷单”质疑 | 极客头条...
  12. java 一个数组key一个数组value_在各种语言中,使用key在map中获取value 和 使用下标获取数组中的数据 相比哪个更快?...
  13. 使用openssl 来生成rsa pkcs1 2048格式的公私钥
  14. 软件工程 选课系统的uml类图_UML学生选课系统.doc
  15. 医学图像有哪些会议期刊可以投
  16. 爆款AR游戏如何打造?网易杨鹏以《悠梦》为例详解前沿技术
  17. 我的世界php motd,MiniMOTD - 服务器列表带有RGB渐变的MOTD插件[1.12.x-1.16.x]【Bukkit】...
  18. Android调取拍照和获取拍照后的照片
  19. linux远程主机拒绝连接,linux – Telnet [无法连接到远程主机:拒绝连接]
  20. 迪厅装修后地板清洁与保养

热门文章

  1. oracle leg函数,032-函数的嵌套与LEGB原则
  2. leetcode59. 螺旋矩阵 II
  3. [mybatis]Configuration XML_typeHandlers
  4. 高等数学下-赵立军-北京大学出版社-题解-练习10.2
  5. [PAT乙级]1007 素数对猜想
  6. LeetCode 501二叉搜索树中的众数-简单
  7. 最长公共子序列-dp
  8. Θ(n)反转单链表(算法导论第三版第十章10.2-7)
  9. Aladdin and the Flying Carpet (素数打表+正整数的唯一分解定理,找因数对)
  10. AVL添加平衡二叉树,是一种二叉排序树,其中每个结点的左子树和右子树的高度差至多等于1。-icoding-数据结构-C-typedef struct node{ int val;