uva10400 - Game Show Math
2013-03-31 16:44
429 查看
看到这道题,确实让我想到了dfs,但是p=100的上限让我望而却步,,,,
后来知道dfs+强剪枝或者称为记忆化搜索dp
这道题为什么记忆化搜索后变得这么高效呢,从4^100>>>64000*100是怎么转变的呢,
原因是这道题的特设条件,(-32000,32000)顶多有64000个状态,每个数有可能在100层中的任何一层的话,那么时间复杂度就变成了64000*100了
这就是记忆化在特定区域内的作用。。。。。
代码如下:
后来知道dfs+强剪枝或者称为记忆化搜索dp
这道题为什么记忆化搜索后变得这么高效呢,从4^100>>>64000*100是怎么转变的呢,
原因是这道题的特设条件,(-32000,32000)顶多有64000个状态,每个数有可能在100层中的任何一层的话,那么时间复杂度就变成了64000*100了
这就是记忆化在特定区域内的作用。。。。。
代码如下:
#include <cstdio> #include <cstring> #define M 105 int st[M], targe, n, pa[M], dp[M][64010]; int dfs(int cur, int value) { if(cur==n) {if(value==targe) return 1; else return 0;} if(dp[cur][value+32000]) return 0; else dp[cur][value+32000] = 1; int tt, t; if(tt=value+st[cur], tt>-32000&&tt<32000&&dfs(cur+1,tt)) { pa[cur] = 1; return 1;} if(tt=value-st[cur], tt>-32000&&tt<32000&&dfs(cur+1,tt)) { pa[cur] = 2; return 1;} if(tt=value*st[cur], tt>-32000&&tt<32000&&dfs(cur+1,tt)) { pa[cur] = 3; return 1;} if(t=value%st[cur], tt=value/st[cur], !t&&tt>-32000&&tt<32000&&dfs(cur+1,tt)) { pa[cur] = 4; return 1;} return 0; } void print_path() { for(int i = 0; i < n; i++) { if(i&&pa[i]==1) printf("+"); if(i&&pa[i]==2) printf("-"); if(i&&pa[i]==3) printf("*"); if(i&&pa[i]==4) printf("/"); printf("%d",st[i]); } printf("=%d\n",targe); } int main () { int t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i = 0; i < n; i++) scanf("%d",&st[i]); scanf("%d",&targe); memset(dp,0,sizeof(dp)); if(dfs(1,st[0])) print_path(); else printf("NO EXPRESSION\n"); } return 0; }
相关文章推荐
- Uva 10400 Game Show Math (DP+记录路径)
- uva10400 - Game Show Math(回溯+剪枝)
- uva 10400 Game Show Math(深搜 )
- UVA - 10400 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 游戏中的数学 dfs+判重
- UVa 10400 - Game Show Math
- uva 10400 - Game Show Math
- UVA - 10400 Game Show Math
- [DFS&&剪枝]uva10400 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
- uva10400 - Game Show Math(回溯+剪枝)
- UVA 10400 Game Show Math
- UVA10400- Game Show Math