首页 > 其他 > 详细

UVA10400- Game Show Math

时间:2014-08-01 23:19:12      阅读:377      评论:0      收藏:0      [点我收藏+]

题意:给出p个数字,使用+,-,*,/,这四个运算符使得最后结果等于n(四个运算符的优先级相同)


思路:回溯+剪枝,不剪枝的话会TLE。用一个标记数组记录在计算的结果。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

const int MAXN = 32000;
const int N = 105;

int arr[N], vis[N][MAXN * 2 + 5];
int n, m, flag;
char path[N];

bool judge(int cur, int sum) {
    return abs(sum) <= MAXN && !vis[cur][sum + MAXN]; 
}

void dfs(int cur, int sum) {
    if (flag)
        return;
    if (cur == n) { 
        if (sum == m) 
            flag = 1; 
        return;
    }
    for (int i = 0; i < 4; i++) {
        if (i == 0 && judge(cur, sum + arr[cur])) {
            vis[cur][sum + arr[cur] + MAXN] = 1; 
            path[cur] = '+';
            dfs(cur + 1, sum + arr[cur]);
        }         
        if (i == 1 && judge(cur, sum - arr[cur])) {
            vis[cur][sum - arr[cur] + MAXN] = 1; 
            path[cur] = '-';
            dfs(cur + 1, sum - arr[cur]);
        } 
        if (i == 2 && judge(cur, sum * arr[cur])) {
            vis[cur][sum * arr[cur] + MAXN] = 1; 
            path[cur] = '*';
            dfs(cur + 1, sum * arr[cur]); 
        }
        if (i == 3 && arr[cur] != 0 && judge(cur, sum / arr[cur])) {
            vis[cur][sum / arr[cur] + MAXN] = 1; 
            path[cur] = '/';
            dfs(cur + 1, sum / arr[cur]); 
        } 
        if (flag)
            return;
    } 
}

void outPut() {
    for (int i = 0; i < n; i++) {
        if (i) 
            printf("%c%d", path[i], arr[i]);
        else
            printf("%d", arr[i]);
    }
    printf("=%d\n", m);
}

int main() {
    int cas;    
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d", &n);    
        for (int i = 0; i < n; i++)
            scanf("%d", &arr[i]);
        scanf("%d", &m); 

        memset(vis, 0, sizeof(vis));
        vis[0][arr[0] + MAXN] = 1;
        flag = 0;
        dfs(1, arr[0]); 

        if (flag) 
            outPut(); 
        else 
            printf("NO EXPRESSION\n");
    }
    return 0;
}


UVA10400- Game Show Math,布布扣,bubuko.com

UVA10400- Game Show Math

原文:http://blog.csdn.net/u011345461/article/details/38341327

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!