uva 10344 23 out of 5 (DFS)

uva 10344 23 out of 5

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

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

            
where : {1,2,3,4,5} -> {1,2,3,4,5} is a bijective function 
and  {+,-,*} (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.

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 

题目大意:加减乘 23(类似游戏加减乘除24)

解题思路:DFS。

#include<stdio.h>
#include<algorithm>
using namespace std;
int num[6], flag;
void DFS(int cnt, int ans) {
	if (cnt == 5) {
		if (ans == 23) flag = 1;
		return;
	}
	DFS(cnt + 1, ans + num[cnt]);
	DFS(cnt + 1, ans - num[cnt]);
	DFS(cnt + 1, ans * num[cnt]);
}
int main() {
	while (scanf("%d %d %d %d %d", &num[0], &num[1], &num[2], &num[3], &num[4]) == 5) {
		if (num[0] == 0 && num[1] == 0 && num[2] == 0 && num[3] == 0 && num[4] == 0) break;
		flag = 0;
		sort(num, num + 5);
		do { DFS(1, num[0]); } while (next_permutation(num, num + 5));
		if (flag) printf("Possible\n");
		else printf("Impossible\n");
	}
}
时间: 2024-08-28 07:26:47

uva 10344 23 out of 5 (DFS)的相关文章

UVa 10344 - 23 out of 5

题目:给你五个数字,在他们中间加上'+','-','*',构成结果23,问能否成功. 分析:搜索,24点类似物. 首先,求出五个数的全排列: 然后,按顺序枚举三种运算,计算结果,判断即可. 说明:注意优先级,左边的高. #include <iostream> #include <cstdlib> #include <cstdio> using namespace std; int data[5],save[5],used[5],buf[5]; int P[120][5]

uva 10344 23 out of 5(递归+全排列)

这道题好像不是回溯就是简单的递归,全排列一下就ok啦,晚上估计不能好好刷题了 代码: #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; int cmp(const void *c,const void *d) { return *(int *)c - *(int *)d ; } int flag; int a[5];

uva 219 - Department of Redundancy Department(dfs+剪枝)

题目链接:uva 219 - Department of Redundancy Department 题目大意:给定一些关系,问哪一些关系是可以被替代的,如果可以被替代,给出替代的方案,一种即可. 解题思路:因为总共也就26个字母,所以用二进制数表示状态.剪枝,每次将所有可选关系均考虑进去都无法满足则是false. #include <cstdio> #include <cstring> #include <algorithm> using namespace std;

UVa 10344 算23点

题意:有5个数,3种运算符:加.减.乘,用全部5个数,和4个运算符,构成一个表达式,使得值为23,这里没有运算符的优先级,全部是从左往右算.是可以这么理解,题目给的是从左到右依次打了括号. 思路:这里是对表达式的位置进行dfs.可以看到第0位是数字,第1位是运算符,依次则,偶数位是数字,奇数位是运算符.dfs中把当前位置cur分为奇偶分别处理.偶数位置时,则对数字进行枚举,并计算当前表达式的值,注意dfs之后的恢复:奇数位置时,枚举三种运算符.对比可以发现,枚举数字时,由于数字只能出现一次,所以

UVA 572 Oil Deposits油田(DFS求连通块)

UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M

UVA - 524 Prime Ring Problem(dfs回溯法)

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number

uva 11008 Antimatter Ray Clearcutting(DFS + 记忆化搜索)

uva 11008 Antimatter Ray Clearcutting It's year 2465, and you are the Chief Engineer for Glorified Lumberjacks Inc. on planet Trie. There is a number of trees that you need to cut down, and the only weapon you have is a high-powered antimatter ray th

【UVA】10317 - Equating Equations(dfs + 剪枝)

真郁闷,一道普通的搜索题 我拿dp的方法去做,结果一直TLE和WA 如果所有数的和为奇数,肯定没有正解. 14133454 10317 Equating Equations Accepted C++ 0.102 2014-09-02 09:01:23 #include <iostream> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #incl

UVA - 10344

Problem I 23 Out of 5 Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Your task is to write a program that can decide whether you can find an arithmetic expression consisting of five given numbers (1<=i<=5) tha