Pots POJ

14 篇文章 0 订阅
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)


这道题结合了前两道题,"非常可乐"和"迷宫问题",先搜索出最少操作步数,然后递归打印操作,代码比较长,敲了一个多小时?(汗... 做法有点奇葩,,本来以为会超时,结果居然AC了~


#include <iostream>  
#include <algorithm> 
#include <cstring> 
#include <cstdio>  
#include <queue>  
  
using namespace std;  
const int N = 100;
  
struct node
{
    int a, b, step;  //a,b分别代表1,2号罐子里的水体积
}operation[N][N];    //记录操作
bool vis[N][N];
int A, B, C;

void output(int i, int j)
{
    if(i == 0 && j == 0)
        return;
    output(operation[i][j].a, operation[i][j].b);   //递归打印每一步操作
    if(operation[i][j].step == 5)
        printf("DROP(1)\n");           //这些数字算是变相的hash吧,,(脑洞有点大
    else if(operation[i][j].step == 6) //取每个操作名的第一个字母在字母表中的顺序加上一个数字, 
        printf("DROP(2)\n");           //比如这里的6,就是4+2,4是DROP的首字母D在字母表中的顺序4,
    else if(operation[i][j].step == 7) //然后加上数字2,就表示DROP(2),即把2号罐子清空
        printf("FILL(1)\n");
    else if(operation[i][j].step == 8)
        printf("FILL(2)\n");
    else if(operation[i][j].step == 16)
        printf("POUR(2,1)\n");
    else
        printf("POUR(1,2)\n");
}
void BFS()
{
    queue<node> q;
    struct node t, u;
    memset(vis, 0, sizeof(vis));
    t.a = 0;
    t.b = 0;
    t.step = 0;
    q.push(t);
    vis[0][0] = 1;
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        if(u.a == C || u.b == C)
        {
            printf("%d\n", u.step);
            output(u.a, u.b);
            return;
        }
        if(u.a != A)     //FILL(1)
        {
            t.a = A;
            t.b = u.b;
            if(!vis[t.a][t.b])
            {
                t.step = u.step + 1;
                q.push(t);
                vis[t.a][t.b] = 1;
                operation[t.a][t.b].a = u.a;
                operation[t.a][t.b].b = u.b;
                operation[t.a][t.b].step = 7;
            }
        }
        if(u.a != 0)     //DROP(1)
        {
            t.a = 0;
            t.b = u.b;
            if(!vis[t.a][t.b])
            {
                t.step = u.step + 1;
                q.push(t);
                vis[t.a][t.b] = 1;
                operation[t.a][t.b].a = u.a;
                operation[t.a][t.b].b = u.b;
                operation[t.a][t.b].step = 5;
            }
        }
        if(u.b != B)     //FILL(2)
        {
            t.a = u.a;
            t.b = B;
            if(!vis[t.a][t.b])
            {
                t.step = u.step + 1;
                q.push(t);
                vis[t.a][t.b] = 1;
                operation[t.a][t.b].a = u.a;
                operation[t.a][t.b].b = u.b;
                operation[t.a][t.b].step = 8;
            }
        }
        if(u.b != 0)     //DROP(2)
        {
            t.a = u.a;
            t.b = 0;
            if(!vis[t.a][t.b])
            {
                t.step = u.step + 1;
                q.push(t);
                vis[t.a][t.b] = 1;
                operation[t.a][t.b].a = u.a;
                operation[t.a][t.b].b = u.b;
                operation[t.a][t.b].step = 6;
            }
        }
        if(u.a && u.b != B)  //POUR(1,2)
        {
            int p = min(u.a, B-u.b);
            t.a = u.a - p;
            t.b = u.b + p;
            if(!vis[t.a][t.b])
            {
                t.step = u.step + 1;
                q.push(t);
                vis[t.a][t.b] = 1;
                operation[t.a][t.b].a = u.a;
                operation[t.a][t.b].b = u.b;
                operation[t.a][t.b].step = 17;
            }
        }
        if(u.b && u.a != A)  //POUR(2,1)
        {
            int p = min(u.b, A-u.a);
            t.a = u.a + p;
            t.b = u.b - p;
            if(!vis[t.a][t.b])
            {
                t.step = u.step + 1;
                q.push(t);
                vis[t.a][t.b] = 1;
                operation[t.a][t.b].a = u.a;
                operation[t.a][t.b].b = u.b;
                operation[t.a][t.b].step = 16;
            }
        }
    }
    printf("impossible\n");  //未实现要求
}
int main()  
{  
    scanf("%d%d%d", &A, &B, &C);
    BFS();
    return 0;  
}  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值