UVA 656 Optimal Programs(bfs)

本文通过BFS算法探讨如何使用五种基本操作(ADD, DIV, DUP, MUL, SUB)对栈进行操作,使得最终栈顶元素等于目标值,并确保所有操作后的元素不超过30000。实现过程包括构建操作转换函数、判断目标是否达成的函数以及通过广度优先搜索找到最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意

给出五个操作,和n个栈,问能否用同一种操作使得栈中最后的数字,等于目标所要的数字,且栈中任意时刻,每个数字不能超过30000。
输出最小的操作数,如果操作数相同,输出字典序最小的。

解析:

可以先bfs第一个数字和目标第一个数字,然后利用这些操作,操作后面的数字,判断结果是否和目标数字一一对应。

AC代码

#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 11;
char str[][5] = {"ADD", "DIV", "DUP", "MUL", "SUB"};
enum  oper_set {ADD, DIV, DUP, MUL, SUB};
struct Node {
    int step, path[N];
    stack<int> st;
    Node() {step = 0;}
    Node trans(int oper) { //5 operator
        Node rhs = *this;
        int a, b;
        if(oper == DUP) {
            a = rhs.st.top();
            rhs.st.push(a);
        }else {
            a = rhs.st.top(); rhs.st.pop();
            b = rhs.st.top(); rhs.st.pop();
            if(oper == ADD) {
                rhs.st.push(a+b);
            }else if(oper == DIV) {
                rhs.st.push(b/a);
            }else if(oper == MUL) {
                rhs.st.push(a*b);
            }else if(oper == SUB) {
                rhs.st.push(b-a);
            }
        }
        return rhs;
    }
};
int x[N], y[N], n, ansPath[N];


bool judge(Node rhs) {
    for(int i = 1; i < n; i++) {
        Node tmp;
        tmp.st.push(x[i]);
        for(int j = 0; j < rhs.step; j++) {
            if(tmp.st.top() == 0 && rhs.path[j] == DIV) return false;
            if(abs(tmp.st.top()) > 30000) return false;
            tmp = tmp.trans(rhs.path[j]);
        }
        if(tmp.st.top() != y[i]) return false;
    }
    return true;
}

int bfs(int star, int end) {
    queue<Node> que;
    Node begin;
    begin.st.push(star); begin.step = 0;
    que.push(begin);
    while(!que.empty()) {
        Node front = que.front(); que.pop();
        if(front.st.size() == 1 && front.st.top() == end) {
            if(judge(front)) {
                memcpy(ansPath, front.path, sizeof(ansPath));
                return front.step;
            }
        }
        for(int i = 0; i < 5; i++) {
            if(front.st.size() == 1 && i != DUP) continue;
            if(front.st.top() == 0 && i == DIV) continue;
            Node tmp = front.trans(i);
            tmp.path[tmp.step++] = i;
            if(abs(tmp.st.top()) > 30000 || tmp.step > 10) continue;
            que.push(tmp);
        }
    }
    return INF;
}

int main() {
    //freopen("in.txt", "r", stdin);
    int cas = 1;
    while(scanf("%d", &n) != EOF && n) {
        memset(x, 0, sizeof(x));
        memset(y, 0, sizeof(y));

        for(int i = 0; i < n; i++)
            scanf("%d", &x[i]);
        for(int i = 0; i < n; i++)
            scanf("%d", &y[i]);

        printf("Program %d\n", cas++);
        if(!memcmp(x, y, sizeof(x))) {
            puts("Empty sequence\n");
            continue;
        }
        int rec = bfs(x[0], y[0]);
        if(rec == INF) {
            puts("Impossible\n");
        }else {
            printf("%s", str[ansPath[0]]);
            for(int i = 1; i < rec; i++) {
                printf(" %s", str[ansPath[i]]);
            }puts("\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值