23 Out of 5 uva+回溯
2014-06-04 22:29
351 查看
23Outof5
Input:standardinput
Output:standardoutput
TimeLimit:1second
MemoryLimit:32MB
Yourtaskistowriteaprogramthatcandecidewhetheryoucanfindanarithmeticexpressionconsistingoffivegivennumbers
(1<=i<=5)thatwillyieldthevalue23.
Forthisproblemwewillonlyconsiderarithmeticexpressionsofthefollowingfrom:
Inputisterminatedbyalinecontainingfivezero's.Thislineshouldnotbeprocessed.
Input:standardinput
Output:standardoutput
TimeLimit:1second
MemoryLimit:32MB
Yourtaskistowriteaprogramthatcandecidewhetheryoucanfindanarithmeticexpressionconsistingoffivegivennumbers
(1<=i<=5)thatwillyieldthevalue23.
Forthisproblemwewillonlyconsiderarithmeticexpressionsofthefollowingfrom:
where
:{1,2,3,4,5}->{1,2,3,4,5}isabijectivefunction
and
{+,-,*}(1<=i<=4)
Input
TheInputconsistsof5-TupelsofpositiveIntegers,eachbetween1and50.Inputisterminatedbyalinecontainingfivezero's.Thislineshouldnotbeprocessed.
Output
Foreach5-Tupelprint"Possible"(withoutquotes)iftheirexistsanarithmeticexpression(asdescribedabove)thatyields23.Otherwiseprint"Impossible".SampleInput
11111
12345
235711
00000
SampleOutput
Impossible
Possible
Possible
解决方案:搞了半天,原来顺序可以换的。
#include<iostream>
#include<cstdio>
#include<cstring>
usingnamespacestd;
inta[5];
boolvis[5];
boolflag;
voiddfs(ints,intsum){
if(s==5){
if(sum==23)flag=true;
return;
}
for(inti=0;i<5;i++){
if(!vis[i]){
vis[i]=true;
if(s==0)
{dfs(s+1,a[i]);}
else{dfs(s+1,sum-a[i]);
dfs(s+1,sum*a[i]);
dfs(s+1,sum+a[i]);}
vis[i]=false;
}
}
}
intmain(){
while(~scanf("%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4])&&(a[0]+a[1]+a[2]+a[3]+a[4])){
flag=false;
memset(vis,false,sizeof(vis));
dfs(0,0);
if(flag)cout<<"Possible\n";
elsecout<<"Impossible\n";
}
return0;
}
相关文章推荐
- UVa 10344 - 23 out of 5 递归回溯
- UVA - 10344 23 out of 5(回溯+全排列)
- UVA 10344 - 23 Out of 5(回溯)
- UVa 10344 23 out of 5 (全排列枚举&回溯)
- UVA 23 Out of 5(DFS+回溯)
- UVa 10344 - 23 out of 5 全排列递归回溯
- UVA 10344 - 23 out of 5 全排列,3进制表示状态,回溯
- [回溯]23 out of 5 UVA 10344
- UVa 10344 - 23 out of 5
- UVA 23 out of 5
- uva 10344 23 out of 5
- UVa 10344 - 23 out of 5, 智力小游戏:算23点
- UVA 10344- 23 out of 5
- UVa 10344 - 23 out of 5
- UVA 10344 23 out of 5
- UVaOJ10344 - 23 out of 5
- UVa 10344 - 23 out of 5
- uva 10344 23 out of 5 凑运算结果 全排列+dfs
- Uva 10344 - 23 out of 5
- UVA 10344 - 23 out of 5