蓝桥杯 Pots(广搜+记录路径)(vj)

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 Cliters 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 Klines 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,一开始都是空的,有以上几种操作,接水或者倒水。问最少进行几次操作,就能使其中一个杯子里的水量为c,并把操作按顺序输出。

很明显的广搜,关键在于如何输出路径。

可以在结构体中加一个vector变量,把每次进行的操作用数字代表,记录下来。这样相当于每个位置都对应了一个vector一长串数,来记录到达这一步要进行的操作。

 

爱吃代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int book[510][510];
string s[6]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct node
{
    int x,y;
    vector<int>v;
};
node n1,n2;
queue<node>q;
int main()
{
    int a,b,c,vis=0,i;
    cin>>a>>b>>c;
    n1.x=0,n1.y=0;
    q.push(n1);
    book[0][0]=1;
    while(!q.empty())
    {
        n1=q.front();
        q.pop();
        if(n1.x==c||n1.y==c)
        {
            vis=1;
            break;
        }
        if(n1.x<a&&book[a][n1.y]==0)
        {
            n2=n1;
            n2.x=a;
            n2.v.push_back(0);
            q.push(n2);
            book[a][n1.y]=1;
        }
        if(n1.y<b&&book[n1.x][b]==0)
        {
            n2=n1;
            n2.y=b;
            n2.v.push_back(1);
            q.push(n2);
            book[n1.x][b]=1;
        }
        if(n1.x>0&&book[0][n1.y]==0)
        {
            n2=n1;
            n2.x=0;
            n2.v.push_back(2);
            q.push(n2);
            book[0][n1.y]=1;
        }
        if(n1.y>0&&book[n1.x][0]==0)
        {
            n2=n1;
            n2.y=0;
            n2.v.push_back(3);
            q.push(n2);
            book[n1.x][0]=1;
        }
        if(n1.y<b)
        {
            n2=n1;
            if(n1.x>b-n1.y&&book[n1.x-(b-n1.y)][b]==0)
            {
                n2.x=n1.x-(b-n1.y);
                n2.y=b;
                book[n2.x][b]=1;
                n2.v.push_back(4);
                q.push(n2);
            }
            else if(n1.x<=b-n1.y&&book[0][n1.y+n1.x]==0)
            {
                n2.x=0;
                n2.y=n1.y+n1.x;
                book[0][n2.y]=1;
                n2.v.push_back(4);
                q.push(n2);
            }
        }
        if(n1.x<a)
        {
            n2=n1;
            if(n1.y>a-n1.x&&book[a][n1.y-(a-n1.x)]==0)
            {
                n2.x=a;
                n2.y=n1.y-(a-n1.x);
                book[a][n2.y]=1;
                n2.v.push_back(5);
                q.push(n2);
            }
            else if(n1.y<=a-n1.x&&book[n1.x+n1.y][0]==0)
            {
                n2.x=n1.x+n1.y;
                n2.y=0;
                book[n2.x][0]=1;
                n2.v.push_back(5);
                q.push(n2);
            }
        }
    }
    if(vis==0)
        cout<<"impossible"<<endl;
    else
    {
        cout<<n1.v.size()<<endl;
        for(i=0;i<n1.v.size();i++)
            cout<<s[n1.v[i]]<<endl;
    }
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值