算法竞赛入门经典 例题7-1 最优程序

本文介绍了一道算法竞赛中的经典题目,并使用BFS(广度优先搜索)方法求解该题。通过实例演示了如何利用C++ STL中的stack和queue实现数据栈的运算,最终找到从初始状态到达目标状态的操作序列。
/*
算法竞赛入门经典 例题7-1 最优程序
BFS,using STL stack, queue!队列的大小指数级增长!
*/
#include <iostream>
#include <string>
#include <cstdio>
#include <queue>
#include <stack>
using namespace std;
const int MAX_OPT = 100;
int opt[MAX_OPT];
char opt_name[][5] = {"ADD", "SUB", "SUL", "DIV", "DUP"};
int a, b;
struct node
{
    string pre;//之前的步骤
    int op; //当前操作 0:ADD, 1:SUB, 2:SUL, 3:DIV, 4DUP
    stack<int> s; //数据栈
};
queue<node> q; //队列
node n;

//对数据栈进行运算
void op()
{
    int a, b;
    if(n.op != 4) {
        a = n.s.top(); n.s.pop();
        b = n.s.top(); n.s.pop();
    } else {
        a = n.s.top();
    }
    switch(n.op)
    {
    case 0: n.s.push(a+b); break;
    case 1: n.s.push(b-a); break;
    case 2: n.s.push(a*b); break;
    case 3: n.s.push(b/a); break;
    case 4: n.s.push(a); break;
    }
    n.pre += (char)(n.op+'0');
}
//清空栈和队列
void clear_st_q()
{
    while(n.s.size()) {
        n.s.pop();
    }
    while(q.size()) {
        q.pop();
    }
}
//宽度遍历
void bfs()
{
    //刚开始只可以进行DUP操作
    n.op = 4;//dup
    n.pre = "";
    n.s.push(a);
    q.push(n);
    while(!q.empty()) {
        n = q.front();
        q.pop();
        op();
        int top = n.s.top();
        if(n.s.top() == b) return;
        for(int i=0; i<5; i++) {
            n.op = i;
            switch(i) {
            case 0:
            case 1:
            case 2:
            case 3:
                if(n.s.size()<2) break;
                if(i==3 && top==0) break;
            case 4:
                q.push(n);
            }
        }
    }
}

int main(void) {
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    //输入a b
    while(scanf("%d%d", &a, &b) == 2) {
        bfs();
        string s = n.pre;
        for(size_t i=0; i<s.size(); i++) {
            printf("%s\n", opt_name[s[i]-'0']);
        }
        printf("\n");
        clear_st_q();
    }
    return 0;
}


测试例子:

5 3
2 1000
2 3
2 4


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值