uva10400 Game Show Math
2014-09-27 16:29
316 查看
这题原本不是很难的,结果我硬是wa了十几次。。。
还是心态的问题,只想快点交题,不想把问题想清楚。
类似于24点游戏,直接 dfs 会超时,于是我就加剪枝。就在这里,我wa了十多次。。。。
本来四种运算的优先级是一样的,只要在dfs前判断余下的数字能得到的最大值的绝对值是不是比target的绝对值大就行了。可我脑残了,把所有的绝对值乘了起来。。。明明才做过类似的题,不一定乘能得到最大值,应该选择加和乘里面绝对值大的,由于不算优先级,这样肯定能得到最大的绝对值。。。渣渣啊,你什么时候能有点进步。
还是心态的问题,只想快点交题,不想把问题想清楚。
类似于24点游戏,直接 dfs 会超时,于是我就加剪枝。就在这里,我wa了十多次。。。。
本来四种运算的优先级是一样的,只要在dfs前判断余下的数字能得到的最大值的绝对值是不是比target的绝对值大就行了。可我脑残了,把所有的绝对值乘了起来。。。明明才做过类似的题,不一定乘能得到最大值,应该选择加和乘里面绝对值大的,由于不算优先级,这样肯定能得到最大的绝对值。。。渣渣啊,你什么时候能有点进步。
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #define MAX 110 using namespace std; int N,s[MAX],target,ok; char ans[MAX]; void dfs(int value,int cur) { //printf("a%d",value); int i; long long sum; if(cur==N) { if(value==target) ok=1; return ; } sum=abs(value); if(target!=0&&value!=0) { for(i=cur;i<N;i++) { /*if(abs(s[i])==1) { if(sum>0) sum+=1; else sum-=1; } else sum*=s[i];*/ if(abs(sum)+abs(s[i])>abs(sum)*abs(s[i]))// sum+=abs(s[i]); else sum*=abs(s[i]); if(sum>=abs(target)) break; } if(i==N) return ; } if(value+s[cur]>=-32000&&value+s[cur]<=32000) { ans[cur-1]='+'; dfs(value+s[cur],cur+1); if(ok==1) return ; } if(value-s[cur]>=-32000&&value-s[cur]<=32000) { ans[cur-1]='-'; dfs(value-s[cur],cur+1); if(ok==1) return ; } if(value*s[cur]>=-32000&&value*s[cur]<=32000) { ans[cur-1]='*'; dfs(value*s[cur],cur+1); if(ok==1) return ; } if(value%s[cur]==0&&s[cur]!=0) { if(value/s[cur]>=-32000&&value/s[cur]<=32000) { ans[cur-1]='/'; dfs(value/s[cur],cur+1); if(ok==1) return ; } } } int main() { int t,i; scanf("%d",&t); while(t--) { ok=0; scanf("%d",&N); for(i=0;i<N;i++) scanf("%d",&s[i]); scanf("%d",&target); dfs(s[0],1); if(ok==0) printf("NO EXPRESSION\n"); else { for(i=0;i<N-1;i++) { printf("%d",s[i]); putchar(ans[i]); } printf("%d=%d\n",s[N-1],target); } } return 0; }
相关文章推荐
- uva 10400(Game Show Math) (DP)
- UVA 10400 Game Show Math
- uva 10400 - Game Show Math
- uva10400 - Game Show Math(回溯+剪枝)
- uva 10400 Game Show Math(深搜 )
- uva10400 - Game Show Math
- uva 10400 Game Show Math
- Game Show Math - UVa10400 搜索
- UVa 10400 - Game Show Math
- UVa 10400 - Game Show Math
- Uva 10400 Game Show Math (DP+记录路径)
- uva 10400 Game Show Math(记忆化搜索)
- UVa 10400 - Game Show Math
- uva 10400 - Game Show Math
- UVa 10400 - Game Show Math 游戏中的数学 dfs+判重
- UVA - 10400 Game Show Math(回溯)
- UVA 10400 Game Show Math (dfs + 记忆化搜索)
- UVa 10400 - Game Show Math
- UVa 10400 - Game Show Math
- Uva 10400 —Game Show Math(深搜+剪枝)