uva656(bfs)

题意:

给出上面一排数字;

下面一排数字;

要求上面的每一个数字,经过相同的步骤变成下面的每一个数字;要求步骤最短;


思路:

bfs;只处理第一个数字,把每种和合法的情况放进队列;

最后得出步骤后,把其他的数照着操作一遍看看;

不能非法操作;

#include <stdio.h> 
#include<cstring>
#include <stack>  
#include <queue>  
using namespace std;  
const int N = 15;  
const char mp[5][10] = {"ADD", "DIV", "DUP", "MUL", "SUB"};  
int n, x[N], y[N];  
  
struct State {  
    stack<int>s;  
    int path[N];  
    int len;  
}p;  
  
queue<State> Q;  

State make(int way, State p) {  
    State q = p;  
    if (way == 0) {  
        int a = q.s.top(); q.s.pop();
        int b = q.s.top(); q.s.pop();
        q.s.push(a + b);  
    }  
    if (way == 4) {  
        int a = q.s.top(); q.s.pop();  
        int b = q.s.top(); q.s.pop();  
        q.s.push(b - a);  
    }  
    if (way == 3) {  
        int a = q.s.top(); q.s.pop();  
        int b = q.s.top(); q.s.pop();  
        q.s.push(a * b);  
    }  
    if (way == 1) {  
        int a = q.s.top(); q.s.pop();  
        int b = q.s.top(); q.s.pop();  
        q.s.push(b / a);  
    }  
    if (way == 2) {  
        int a = q.s.top();  
        q.s.push(a);  
    }  
    q.path[q.len++] = way;  
    return q;  
}  
  
bool judge(State p) {  
    for (int i = 1; i < n; i ++) {  
        State q;
	   	q.len = 0;
	   	q.s.push(x[i]);
	   	memset(q.path, 0, sizeof(q.path));  
        for (int j = 0; j < p.len; j++) {  
            if (p.path[j] == 1 && q.s.top() == 0 || q.s.top() > 30000 || q.s.top() < -30000 ) return false;  
            q = make(p.path[j], q);  
        }  
        if (q.s.top() != y[i]) return false;  
    }  
    return true;  
}  
  
bool bfs() {  
    while(!Q.empty()) 
		Q.pop();  
    while(!p.s.empty()) 
		p.s.pop();  
    p.s.push(x[0]);
   	p.len = 0; 
	memset(p.path, 0, sizeof(p.path));  
    Q.push(p);  
    while (!Q.empty()) {  
        p = Q.front();
	   	Q.pop();  
        if (p.s.size() == 1 && p.s.top() == y[0]) {  
            if (judge(p)) 
				return true;  
        } 
        for (int i = 0; i < 5; i ++) {  
            int t = p.s.size() - (10 - p.len);
            if (i == 2 && t > 1) continue;  
            if (p.s.size() == 1 && i != 2) continue;  
            if (i == 1 && p.s.top() == 0) continue;  
            if(p.len >= 10) continue;  
            State q = make(i, p);  
            if(q.s.top() >= -30000 && q.s.top() <= 30000)  
                Q.push(q);  
        }  
    }  
    return false;  
}  
  
void solve() {  
    if (bfs()) {  
        if (p.len == 0) 
			printf("Empty sequence\n");  
        else {  
            for (int i = 0; i < p.len - 1; i ++)  
                printf("%s ", mp[p.path[i]]);  
            printf("%s\n", mp[p.path[p.len - 1]]);  
        }  
    }  
    else printf("Impossible\n");  
    printf("\n");  
}  
  
int main() {  
    int cas = 1;  
    while (scanf("%d", &n) && n) {  
		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++);  
		solve();  
	}  
	return 0;  
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值