【POJ3414】Pots(倒水问题+输出路径)

题目链接

Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:22897   Accepted:9718   Special Judge

Description

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 ≤ ≤ 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 AB, 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)

Source

Northeastern Europe 2002, Western Subregion

【题意】

已知两个不知道刻度的杯子A,B,根据已知操作判断是否可以倒出C的水,如果可以输出最少的倒水次数和倒水方案,若不行则输出impossible。

【解题思路】

常规倒水问题,因为需要输出路径,所以在结构体内定义一个string数组,用来存储每次的操作。

倒水的模拟过程详见:https://blog.csdn.net/qq_39826163/article/details/82694161

【代码】

#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=1005;
int vis[maxn][maxn];
int a,b,c;
struct Node
{
    int a,b,step;
    string s[maxn];
}ans;
int check(Node )
int bfs()
{
    Node node;
    memset(vis,0,sizeof(vis));
    node.a=0;node.b=0;node.step=0;
    queue<Node>q;
    q.push(node);
    while(!q.empty())
    {
        Node t=q.front(),p;
        q.pop();
        if(t.a==c || t.b==c)
        {
            ans=t;
            return 1;
        }
        if(!vis[a][t.b])
        {
            p=t;
            p.step++;
            p.a=a;
            p.s[p.step]="FILL(1)";
            vis[p.a][p.b]=1;
            q.push(p);
        }
        if(!vis[t.a][b])
        {
            p=t;
            p.step++;
            p.b=b;
            p.s[p.step]="FILL(2)";
            vis[p.a][p.b]=1;
            q.push(p);
        }
        if(!vis[0][t.b])
        {
            p=t;
            p.step++;
            p.a=0;
            p.s[p.step]="DROP(1)";
            vis[p.a][p.b]=1;
            q.push(p);
        }
        if(!vis[t.a][0])
        {
            p=t;
            p.step++;
            p.b=0;
            p.s[p.step]="DROP(2)";
            vis[p.a][p.b]=1;
            q.push(p);
        }
        if(!vis[(a-t.a>=t.b)?t.a+t.b:a][(a-t.a>=t.b)?0:t.b-a+t.a])
        {
            p=t;
            p.step++;
            p.a=(a-t.a>=t.b)?t.a+t.b:a;
            p.b=(a-t.a>=t.b)?0:t.b-a+t.a;
            p.s[p.step]="POUR(2,1)";
            vis[p.a][p.b]=1;
            q.push(p);
        }
        if(!vis[(b-t.b>=t.a)?0:t.a-b+t.b][(b-t.b>=t.a)?t.a+t.b:b])
        {
            p=t;
            p.step++;
            p.a=(b-t.b>=t.a)?0:t.a-b+t.b;
            p.b=(b-t.b>=t.a)?t.a+t.b:b;
            p.s[p.step]="POUR(1,2)";
            vis[p.a][p.b]=1;
            q.push(p);
        }
    }
    return 0;
}
int main()
{
    scanf("%d%d%d",&a,&b,&c);
    if(!bfs())printf("impossible\n");
    else
    {
        printf("%d\n",ans.step);
        for(int i=1;i<=ans.step;i++)
            cout<<ans.s[i]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39826163/article/details/82771070