uva 10400 - Game Show Math
2014-06-19 12:57
441 查看
#include <cstdio> #include <cstring> #include <cctype> #include <cstdlib> #include <queue> #include <stack> #include <cmath> #include <string> #include <iostream> #include <map> #include <algorithm> using namespace std; #define maxn 100 + 10 #define INF 2100000000 #define ll long long bool ok; int ans; int fis[maxn]; char sy[4] = {'+', '-', '*', '/'}; bool vis[maxn][64010]; int n, a[maxn], s[maxn]; bool judgs(int m, int cur) { if(m >= -32000 && m <= 32000 && !vis[cur][m]) return true; return false; } void dfs(int cur, int sum) { if(ok) return; if(cur == n) { if(sum == ans) { ok = true; for(int i = 1; i < n; ++i) fis[i] = s[i]; } return; } s[cur] = 0; if(judgs(sum+a[cur], cur+1)) dfs(cur+1, sum+a[cur]), vis[cur+1][sum+a[cur]] = true; s[cur] = 1; if(judgs(sum-a[cur], cur+1)) dfs(cur+1, sum-a[cur]), vis[cur+1][sum-a[cur]] = true; s[cur] = 2; if(judgs(sum*a[cur], cur+1)) dfs(cur+1, sum*a[cur]), vis[cur+1][sum*a[cur]] = true; s[cur] = 3; if(a[cur]!=0 && sum%a[cur] == 0 && judgs(sum/a[cur], cur+1)) dfs(cur+1, sum/a[cur]); return; } int main() { int t; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i = 0; i < n; ++i) scanf("%d", &a[i]); scanf("%d", &ans); ok = false; memset(vis, false, sizeof(vis)); dfs(1, a[0]); if(ok) { printf("%d", a[0]); for(int i = 1; i < n; ++i) printf("%c%d", sy[fis[i]], a[i]); printf("=%d\n", ans); } else puts("NO EXPRESSION"); } return 0; }
相关文章推荐
- UVA - 10400 Game Show Math
- [DFS&&剪枝]uva10400 Game Show Math
- uva10400 - Game Show Math(回溯+剪枝)
- UVA 10400 - Game Show Math
- UVA10400- Game Show Math
- UVA 10400 Game Show Math
- uva 10400 - Game Show Math
- uva 10400 Game Show Math
- UVA - 10400 Game Show Math
- UVA 10400 Game Show Math
- uva 10400(Game Show Math) (DP)
- uva10400 Game Show Math
- uva10400 - Game Show Math(回溯+剪枝)
- uva 10400 Game Show Math(深搜 )
- uva10400 - Game Show Math
- uva 10400 Game Show Math
- UVa 10400 - Game Show Math
- Uva 10400 Game Show Math (DP+记录路径)
- Game Show Math - UVa10400 搜索
- UVa 10400 - Game Show Math