UVA 656 Optimal Programs(bfs)

题意

给出五个操作,和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;
}

你可能感兴趣的:(uva,656)