码迷,mamicode.com
首页 > 其他好文 > 详细

uva 817(dfs)

时间:2015-05-01 16:08:30      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

题意:给出一个数字组成的字符串,然后在字符串内添加三种运算符号 * + - ,要求输出所有添加运算符并运算后结果等于2000的式子。
所有数字不能有前导0,且式子必须是合法的。
题解:字符串长度25左右,可以暴力,用dfs搜索所有可能的分割情况,并在每次分割的字符后添加三种运算符,然后递归到最后一个字符拿去判断,先用栈把所有分割字符串得到的数字压栈,同时优先运算’*’,然后再从左到右计算看结果是否为2000,注意2000=是IMPOSSIBLE。。。

#include <stdio.h>
#include <string.h>
#include <vector>
#include <stack>
using namespace std;
const int N = 30;
char str[N], s1[N], flag[3] = {‘*‘, ‘+‘, ‘-‘};
int res, len, s[N], pos[N];
vector<char> v[100];

bool judge(int num) {
    int cnt = 0, temp = 0, flag2 = 0;
    stack<int> sta;
    while (!sta.empty())
        sta.pop();
    for (int i = 0; i < len; i++) {
        temp = temp * 10 + s[i];
        if (i == pos[cnt]) {
            if (flag2) {
                int aa = sta.top();
                sta.pop();
                aa = aa * temp;
                sta.push(aa);
                if (s1[cnt] != ‘*‘)
                    flag2 = 0;
            }
            else if (s1[cnt] == ‘+‘ || s1[cnt] == ‘-‘)
                sta.push(temp);
            else if (s1[cnt] == ‘*‘) {
                sta.push(temp);
                flag2 = 1;
            }
            temp = 0;
            cnt++;
        }
    }
    if (flag2) {
        int aa = sta.top();
        sta.pop();
        aa = aa * temp;
        sta.push(aa);
    }
    else
        sta.push(temp);
    stack<int> sta2;
    while (!sta.empty()) {
        sta2.push(sta.top());
        sta.pop();
    }
    for (int i = 0; i < num; i++) {
        if (s1[i] != ‘*‘) {
            int aa = sta2.top();
            sta2.pop();
            int bb = sta2.top();
            sta2.pop();
            if (s1[i] == ‘-‘) {
                int cc = aa - bb;
                sta2.push(cc);
            }
            else {
                int cc = aa + bb;
                sta2.push(cc);
            }
        }
    }
    if (sta2.size() == 1 && sta2.top() == 2000)
        return true;
    return false;
}

void dfs(int cur, int num, int pre) {
    if (cur == len - 1) {
        if (judge(num)) {
            v[res].clear();
            int cnt = 0;
            for (int i = 0; i < len; i++) {
                v[res].push_back(str[i]);
                if (i == pos[cnt])
                    v[res].push_back(s1[cnt++]);
            }
            v[res].push_back(‘=‘);
            res++;
        }
        return;
    }
    pos[num] = cur;
    for (int i = 0; i < 3; i++) {
        s1[num] = flag[i];
        dfs(cur + 1, num + 1, cur);
    }
    pos[num] = -1;
    if (str[cur] == ‘0‘ && cur - pre == 1)
        return;
    dfs(cur + 1, num, pre);
}

int main() {
    int cas = 1;
    while (scanf("%s", str) && str[0] != ‘=‘) {
        memset(pos, -1, sizeof(pos));
        res = 0;
        len = strlen(str) - 1;
        for (int i = 0; i < len; i++)
            s[i] = str[i] - ‘0‘;
        printf("Problem %d\n", cas++);
        if (strcmp(str, "2000=") == 0) {
            printf("  IMPOSSIBLE\n");
            continue;
        }
        dfs(0, 0, -1);
        if (res == 0)
            printf("  IMPOSSIBLE\n");
        else {
            for (int i = 0; i < res; i++) {
                printf("  %c", v[i][0]);
                for (int j = 1; j < v[i].size(); j++)
                    printf("%c", v[i][j]);
                printf("\n");
            }
        }
    }
    return 0;
}

uva 817(dfs)

标签:

原文地址:http://blog.csdn.net/hyczms/article/details/45419585

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!