H - Pots

题目:
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

代码如下:

#include<iostream>
#include<queue>
#include<stack>
#include<cstring>
using namespace std;
int A,B,C,ans;
struct Node
{
    int x,y;   //代表两个杯中液体体积
    int step;   //走到目前所用步数
    int num;    //上一步走到这步的步骤
    Node *next;
};
bool vis[150][150];
stack<int> s;
void bfs(int x,int y)
{
    memset(vis,true,sizeof(vis));
    queue<Node> q;
    Node a;
    Node t[400];
    a.x = 0;
    a.y = 0;
    a.step = 0;
    a.num = 0;
    a.next = NULL;
    vis[0][0] = false;
    q.push(a);
    int count = -1;
    while(!q.empty())
    {
        count++;
        t[count] = q.front();
        q.pop();
        for(int i = 1;i <= 6;i++)
        {
            switch(i)
            {
                case 1:
                    a.x = A;
                    a.y = t[count].y;
                    a.num = 1;
                    break;
                case 2:
                    a.x = t[count].x;
                    a.y = B;
                    a.num = 2;
                    break;
                case 3:
                    a.x = 0;
                    a.y = t[count].y;
                    a.num = 3;
                    break;
                case 4:
                    a.x = t[count].x;
                    a.y = 0;
                    a.num = 4;
                    break;
                case 5:
                    if(t[count].x > B - t[count].y)
                    {
                        a.x = t[count].x - (B - t[count].y);
                        a.y = B;
                    }
                    else
                    {
                        a.x = 0;
                        a.y = t[count].x + t[count].y;
                    }
                    a.num = 5;
                    break;
                case 6:
                    if(t[count].y > A - t[count].x)
                    {
                        a.x = A;
                        a.y = t[count].y - (A - t[count].x);
                    }
                    else
                    {
                        a.x = t[count].x + t[count].y;
                        a.y = 0;
                    }
                    a.num = 6;
                    break;
            }
            if(!vis[a.x][a.y]) continue;
            vis[a.x][a.y] = false;
            a.step = t[count].step + 1;
            a.next = &t[count];
            if(a.x == C || a.y == C)
            {
                ans = a.step;
                while(a.next != NULL)
                {
                    s.push(a.num);
                    a = *a.next;
                }
                return;
            }
            q.push(a);
        }
    }
}

void print()
{
    while(!s.empty())
    {
        int num = s.top();
        s.pop();
        switch(num)
        {
            case 1:
                cout << "FILL(1)" << endl;
                break;
            case 2:
                cout << "FILL(2)" << endl;
                break;
            case 3:
                cout << "DROP(1)" << endl;
                break;
            case 4:
                cout << "DROP(2)" << endl;
                break;
            case 5:
                cout << "POUR(1,2)" << endl;
                break;
            case 6:
                cout << "POUR(2,1)" << endl;
                break;
        }
    }
}

int main()
{
    cin >> A >> B >> C;
    bfs(0,0);
    if(!ans) cout << "impossible" << endl;
    else
    {
        cout << ans << endl;
        print();
    }
    return 0;
}

先输入2个杯子的体积,在输入想要达到的体积,可以执行6种不同的操作,当任意一个杯子中液体体积达到目标值时停止。输出步骤数,以及具体步骤。

这是一道bfs + 保存路径题,每次bfs尝试不同步骤时需要将上一步保存下来,这里用指针来完成。当达到目标值时,需要把之前步骤存放在栈中,最后输出步骤数,以及具体步骤。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值