H - Pots POJ - 3414

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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)

题意:

给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作

FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2);

DROP(i)        将第i个容器抽干

POUR(i,j)      将第i个容器里的水倒入第j个容器(这次操作结束后产生两种结果,一是第j个容器倒满并且第i个容器依旧有剩余,二是第i个容器里的水全部倒入j中,第i个容器为空)

现在要求你写一个程序,来找出能使其中任何一个容器里的水恰好有C升,找出最少操作数并给出操作过程

Input

有且只有一行,包含3个数A,B,C(1<=A,B<=100,C<=max(A,B))

Output

第一行包含一个数表示最小操作数K

随后K行每行给出一次具体操作,如果有多种答案符合最小操作数,输出他们中的任意一种操作过程,如果你不能使两个容器中的任意一个满足恰好C升的话,输出“impossible”

题解:找出最小步数可以直接用BFS,分为6种不同的情况。但是找出每一步的路线不太好做。我是在结构体node里定义了flag代表当前是哪种情况,用pre代表上一步在数组G中的下标(即位置),再回溯路线,用stack存储路径,最后一个一个输出。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 500;
int A,B,C;
int vis[maxn][maxn];//标记走过的路径
int turn;//标记是否找到

typedef struct NODE
{
    int a,b;
    int step;
    int flag;//是哪种方式
    int pre;//前一个路径的下标
}node;

void bfs()
{
    turn = 0;
    node N;
    node G[maxn];//存储各个不同的路径
    int tot = 0;
    N.a = 0;
    N.b = 0;
    N.step = 0;
    vis[0][0] = 1;
    queue<node>Q;
    Q.push(N);

    while(!Q.empty())
    {
        N = Q.front();
        G[++tot] = N;
        node M;
        Q.pop();
        if(N.a == C || N.b == C)
        {
            printf("%d\n",N.step);
            stack<int>P;
            while(N.a != 0 || N.b != 0)//回溯路径
            {
                P.push(N.flag);
                N = G[N.pre];
            }
            while(!P.empty())//输出路径
            {
                int f = P.top();
                P.pop();
                if(f == 1)
                {
                    printf("FILL(1)\n");
                }
                else if(f == 2)
                {
                    printf("FILL(2)\n");
                }
                else if(f == 3)
                {
                    printf("DROP(1)\n");
                }
                else if(f == 4)
                {
                    printf("DROP(2)\n");
                }
                else if(f == 5)
                {
                    printf("POUR(1,2)\n");
                }
                else
                {
                    printf("POUR(2,1)\n");
                }
            }
            turn = 1;
            break;
        }

        if(N.a != A)                        //第一种情况
        {
            M.a = A;
            M.b = N.b;
            M.step = N.step + 1;
            M.pre = tot;
            M.flag = 1;
            if(!vis[M.a][M.b])
            {
                Q.push(M);
                vis[M.a][M.b] = 1;
            }
        }

        if(N.b != B)                        //第二种情况
        {
            M.a = N.a;
            M.b = B;
            M.step = N.step + 1;
            M.pre = tot;
            M.flag = 2;
            if(!vis[M.a][M.b])
            {
                Q.push(M);
                vis[M.a][M.b] = 1;
            }
        }

        if(N.a)                             //第三种情况
        {
            M.a = 0;
            M.b = N.b;
            M.step = N.step + 1;
            M.pre = tot;
            M.flag = 3;
            if(!vis[M.a][M.b])
            {
                Q.push(M);
                vis[M.a][M.b] = 1;
            }
        }

        if(N.b)                             //第四种情况
        {
            M.a = N.a;
            M.b = 0;
            M.step = N.step + 1;
            M.pre = tot;
            M.flag = 4;
            if(!vis[M.a][M.b])
            {
                Q.push(M);
                vis[M.a][M.b] = 1;
            }
        }

        if(N.a < B - N.b)                   //第五种情况
        {
            M.a = 0;
            M.b = N.b + N.a;
            M.step = N.step + 1;
            M.pre = tot;
            M.flag = 5;
            if(!vis[M.a][M.b])
            {
                Q.push(M);
                vis[M.a][M.b] = 1;
            }
        }
        else
        {
            M.a = N.a - (B - N.b);
            M.b = B;
            M.step = N.step + 1;
            M.pre = tot;
            M.flag = 5;
            if(!vis[M.a][M.b])
            {
                Q.push(M);
                vis[M.a][M.b] = 1;
            }
        }

        if(N.b < A - N.a)                   //第六种情况
        {
            M.a = N.b + N.a;
            M.b = 0;
            M.step = N.step + 1;
            M.pre = tot;
            M.flag = 6;
            if(!vis[M.a][M.b])
            {
                Q.push(M);
                vis[M.a][M.b] = 1;
            }
        }
        else
        {
            M.a = A;
            M.b = N.b - (A - N.a);
            M.step = N.step + 1;
            M.pre = tot;
            M.flag = 6;
            if(!vis[M.a][M.b])
            {
                Q.push(M);
                vis[M.a][M.b] = 1;
            }
        }
    }
    if(!turn) printf("impossible\n");
}

int main()
{
    while(scanf("%d%d%d",&A,&B,&C) != EOF)
    {
        memset(vis,0,sizeof(vis));
        bfs();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Eric_chen_song_lin/article/details/81878251