Your task is to write a program that can decide whether you can find an arithmetic expression consisting of five given numbers ai (1 ≤ i ≤ 5) that will yield the value 23.

For this problem we will only consider arithmetic expressions of the following from:

(((aπ(1) o1 aπ(2)) o2 aπ(3)) o3 aπ(4)) o4 aπ(5)

where π : {1, 2, 3, 4, 5} → {1, 2, 3, 4, 5} is a bijective function and oi ∈ {+, −, ∗}(1 ≤ i ≤ 4)

Input

The Input consists of 5-Tupels of positive Integers, each between 1 and 50.

Input is terminated by a line containing five zero’s. This line should not be processed. Input file will have no more than 25 lines.

Output

For each 5-Tupel print ‘Possible’ (without quotes) if their exists an arithmetic expression (as described above) that yields 23. Otherwise print ‘Impossible’.

Sample Input

1 1 1 1 1

1 2 3 4 5

2 3 5 7 11

0 0 0 0 0

Sample Output

Impossible

Possible

Possible

问题链接:UVA10344 23 out of 5

问题描述

输入5个数,按照指定的公式,用运算符{+,-*}进行计算,判断其结果是否有可能为23。

问题分析

暴力是必须的,问题在于怎么实现暴力。

用DFS实现是一种有效的方法,其中用到递归和回溯。

另外一种做法是使用全排列实现。操作数先做一下全排列,然后再对运算符进行暴力搜索,也许程序逻辑可以稍微简单一些。

程序说明

给出2种程序,可以对照着看。

第一个程序的第16行条件,加上程序可以加快程序结束。

使用全排列函数进行计算似乎是需要先排序的!

参考链接:(略)

题记:需要暴力时,考虑一下全排列。

AC的C++语言程序如下:

/* UVA10344 23 out of 5 */#include <bits/stdc++.h>using namespace std;const int N = 5;
int a[N];
bool flag, vis[N];void dfs(int cnt, int result)
{if (cnt == N) {if (result == 23)flag = true;} else if(!flag) {for (int i = 0; i < N; i++) {if (!vis[i]) {vis[i] = true;dfs(cnt + 1, result + a[i]);dfs(cnt + 1, result - a[i]);dfs(cnt + 1, result * a[i]);vis[i] = 0;}}}
}int main()
{for(; ;) {int sum = 0;for(int i = 0; i < N; i++) {scanf("%d", &a[i]);sum += a[i];}if(sum == 0)break;flag = false;memset(vis, false, sizeof(vis));for(int i = 0; i < N; i++) {vis[i] = true;dfs(1, a[i]);vis[i] = false;if(flag)break;}printf("%s\n", flag ? "Possible" : "Impossible");}return 0;
}

AC的C++语言程序如下:

/* UVA10344 23 out of 5 */#include <bits/stdc++.h>using namespace std;const int N = 5;
int a[N];bool dfs(int cnt, int result)
{if(cnt == N) {if(result == 23)return true;} elsereturn dfs(cnt + 1, result + a[cnt]) || dfs(cnt + 1, result - a[cnt]) || dfs(cnt + 1, result * a[cnt]);return false;
}int main()
{for(; ;) {int sum = 0;for(int i = 0; i < N; i++) {scanf("%d", &a[i]);sum += a[i];}if(sum == 0)break;bool flag;sort(a, a + N);do {if((flag = dfs(1, a[0])))break;} while(next_permutation(a, a + N));printf("%s\n", flag ? "Possible" : "Impossible");}return 0;
}

UVA10344 23 out of 5【暴力+DFS】相关推荐

  1. CSP-何以包邮?(暴力DFS、背包问题)

    题目描述 新学期伊始,适逢顿顿书城有购书满 x 元包邮的活动,小 P 同学欣然前往准备买些参考书. 一番浏览后,小 P 初步筛选出 n 本书加入购物车中,其中第 i 本(1≤i≤n)的价格为 ai 元 ...

  2. 【洛谷 - U43391】不是0-1背包的暴力AC(思维,二分,可转化为二元组问题,复习暴力dfs总结)

    题干: https://www.luogu.org/problemnew/show/U43391 自01背包问世之后,小A对此深感兴趣.一天,小A去远游,却发现他的背包不同于01背包. 小A的背包最多 ...

  3. 紫书搜索 习题7-4 UVA - 818 Cutting Chains 暴力+dfs判环+位运算

    题目链接: https://vjudge.net/problem/UVA-818 题意: 选几个圆环去open.然后该圆环和其他就断开了.然后用这些open的圆环去连接剩下的圆环[最后打开的会合上], ...

  4. uva1509(暴力dfs)

    题意: 给出两个字符串,第一个字符串中的每个字符(只包括小写字母)最多可以变成k(k<=3)个字符(不一定是小写字母),相同字母要变成相同的字符,问由第一个串变成第二个串合不合法. 思路: 串的 ...

  5. [蓝桥杯][2013年第四届真题]危险系数(暴力+dfs)

    题目描述 问题描述 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点间可能因此会失去联系. 我们来定义一个危险 ...

  6. 【noip模拟赛4】Matrix67的派对 暴力dfs

    [noip模拟赛4]Matrix67的派对 描述 Matrix67发现身高接近的人似乎更合得来.Matrix67举办的派对共有N(1<=N<=10)个人参加,Matrix67需要把他们安排 ...

  7. UVA 818 Cutting Chains 切断圆环链 (暴力dfs)

    题意就是给一张无向图,去掉某些结点,然后连成一条链,问最少去掉几个结点. n很小n<=15,所以直接枚举2^15个状态就行啦. 链的条件是1.无环,2.没有度大于2的点,3.把n个散链连起来需要 ...

  8. bzoj 3752: Hack 预处理+暴力dfs

    题目大意: 定义字符串的hash值\(h = \sum_{i=0}^{n-1}p^{n-i-1}s_i\) 现在给定K个长度不超过L的字符串S,对于每个字符串S,求字典序最小长度不超过L的字符串T使得 ...

  9. bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐(暴力DFS)

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 774  Solved: 480 ...

最新文章

  1. 四个Webix实例:生成多种类型的JavaScript列表
  2. 事件委托能够优化js性能
  3. ELK 企业级日志分析系统
  4. 代码单元测试:gtest
  5. MyEclipse的Git配置
  6. 如何在SQL Server中使用级联删除?
  7. 【AMAD】schema -- 使用pythonic的方式进行schema验证
  8. 图片url地址的生成获取方法
  9. java教程 doc,java 基础教程.doc
  10. Java实现:四六级真题批量PDF文件英文单词词频分析、排序
  11. 串行接口的工作原理和实现
  12. java excel cell 设置样式_java中对Excel的创建、样式修改
  13. mongodb 集群shard_MongoDBV3.0.7版本(shard+replica)集群的搭建及验证
  14. PTN学习总结---IP基础
  15. xposed插件微信机器人
  16. CSS 在table td一段文字前面做一个空白小框,空白下划线
  17. Vivado Turtorial 01 —— 使用vivado中debug功能(类似ISE中ChipScope)
  18. 《大话设计模式(C#实现)》(Yanlz+VR云游戏+Unity+SteamVR+云技术+5G+AI+设计模式+GoF+UML+单例模式+观察者模式+抽象工厂+代理模式+框架编程+立钻哥哥++OK+)
  19. 坑爹的青云志(第一集)
  20. 公开课|“隐私计算+区块链”安全解锁数据价值

热门文章

  1. 基于已有集群动态发现方式部署 Etcd 集群
  2. Android ListView 实现下拉刷新上拉加载
  3. Android中ListView数据处理优化
  4. 在WinMain中嵌Console窗口
  5. python卸载_如何为Python程序制作Windows安装包?
  6. java退出登录_java实现注销登录
  7. win10远程计算机或设备将不接收连接?
  8. C++小白课本练习4
  9. linux格式化外接硬盘命令,linux格式化硬盘命令
  10. Android 裁切踩坑