poj3414 Pots

记录a,b水位分别作为x,y来存储bfs扩展出的全图
分为六种情况,然后按照每种情况来拓展,最后得到答案

#include<iostream>
#include<queue>
#include<stack>
using namespace std;
int a, b, c;
struct MyStruct
{
    int a, b;//记录a,b水位
    int step;//记录多少步
    int status;//记录状态
    int prex;//记录前一个x
    int prey;//记录前一个y
};
queue<MyStruct>que;
MyStruct ans;
MyStruct id[10010][1010];//记录bfs扩展出的图
int vis[1010][1010];//记录是否访问过
int flag;//标记
void bfs() {//标准bfs
    MyStruct t;
    t.a = 0;
    t.b = 0;
    t.status = 0;
    t.step = 1;
    id[0][0] = t;
    que.push(t);
    while (!que.empty()) {
        MyStruct now, next;
        now = que.front();
        que.pop();
        if (now.a == c || now.b == c) {
            ans = now;
            flag = 1;
            return;
        }
        for (int i = 0; i < 6; i++) {//分为6种情况
            if (i == 0) {//a满
                next.a = a;
                next.b = now.b;
            }
            else if (i == 1) {//b满
                next.a = now.a;
                next.b = b;
            }
            else if (i == 2) {//a空
                next.a = 0;
                next.b = now.b;
            }
            else if (i == 3) {
                next.a = now.a;
                next.b = 0;
            }
            else if (i == 4) {//a装b
                if (now.a + now.b <= b) {//小于等于
                    next.a = 0;
                    next.b = now.a + now.b;
                }
                else {//大于
                    next.a = now.a + now.b - b;
                    next.b = b;
                }
            }
            else{//b装a
                if (now.b + now.a <= a) {//小于等于
                    next.a = now.a + now.b;
                    next.b = 0;
                }
                else {//大于
                    next.b = now.a + now.b - a;
                    next.a = a;
                }
            }
            next.status = i;
            if (vis[next.a][next.b] == 0) {//入队
                vis[next.a][next.b] = 1;
                next.step = now.step + 1;//步数+1
                next.prex = now.a;//记录前一个x
                next.prey = now.b;//记录前一个y
                id[next.a][next.b] = next;//记录扩展出的图
                que.push(next);
            }
        }
    }
}
int main() 
{
    while (cin >> a >> b >> c) {
        stack<int>num;
        flag = 0;
        bfs();
        if (flag) {
            MyStruct t = ans;
            for (int i = ans.step-1; i >0; i--) {
                int x, y;
                x = t.prex;
                y = t.prey;
                num.push(t.status);
                t = id[x][y];
            }
            cout << num.size()<< endl;
            while (!num.empty()) {
                int T = num.top();
                num.pop();
                if (T == 0) {
                    cout << "FILL(1)" << endl;
                }
                else if (T == 1) {
                    cout << "FILL(2)" << endl;
                }
                else if (T == 2) {
                    cout << "DROP(1)" << endl;
                }
                else if (T == 3) {
                    cout << "DROP(2)" << endl;
                }
                else if (T == 4) {
                    cout << "POUR(1,2)" << endl;
                }
                else if (T == 5) {
                    cout << "POUR(2,1)" << endl;
                }
            }
        }
        else {
            cout << "impossible" << endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值