POJ3414 Pots BFS

裸地BFS,要求输出路径,用一个数组保存ID和pre即可

判重的时候用一个二维数组,分别表示第一个杯子和第二个杯子的水有多少。

对一个节点有6种操作,分别针对两个杯子的3中不同操作,1:一个杯子的水倒出来 2 把一个杯子倒满 3 把一个杯子的水倒到另一个里面(这里注意能到多少)


Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7913 Accepted: 3326 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)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>

using namespace std;

int vis[110][110];
int b[2],c;

struct node
{
    int a[2],t,sol,s;
    int pre,id;
}data[10000];

int p;

bool jud(node k)
{
    if(vis[k.a[0]][k.a[1]]==1)
        return false;
    return true;
}

node bfs()
{
    node tmp,k;
    queue<node> q;
    p=0;
    data[p].a[0]=0,data[p].a[1]=0,data[p].t=0,data[p].sol=-1,data[p].s=0;
    data[p].pre=-1,data[p].id=p;
    q.push(data[p]);
    p++;
    while(!q.empty())
    {
        tmp=q.front();
        q.pop();
        for(int i=0;i<2;i++)
        {
            k=tmp;
            k.t++;
            k.a[i]=b[i];
            k.s=i;
            k.sol=0;
            if(jud(k))
            {
                vis[k.a[0]][k.a[1]]=1;
                data[p]=k;
                data[p].id=p,data[p].pre=tmp.id;
                if(data[p].a[0]==c || data[p].a[1]==c)
                    return data[p];
                q.push(data[p]);
                p++;
            }
            k=tmp;
            k.t++;
            k.a[i]=0;
            k.s=i;
            k.sol=1;
            if(jud(k))
            {
                vis[k.a[0]][k.a[1]]=1;
                data[p]=k;
                data[p].id=p,data[p].pre=tmp.id;
                if(data[p].a[0]==c || data[p].a[1]==c)
                    return data[p];
                q.push(data[p]);
                p++;
            }
            k=tmp;
            int s=i,d=i^1,dis;
            dis=b[d]-k.a[d];
            k.t++;
            if(k.a[s]>=dis)
                k.a[s]-=dis;
            else
                dis=k.a[s],k.a[s]=0;
            k.a[d]+=dis;
            k.s=s;
            k.sol=2;
            if(jud(k))
            {

                vis[k.a[0]][k.a[1]]=1;
                data[p]=k;
                data[p].id=p,data[p].pre=tmp.id;
                if(data[p].a[0]==c || data[p].a[1]==c)
                    return data[p];
                q.push(data[p]);
                p++;
            }
        }
    }
    k.t=-1;
    return k;
}

void out(int p)
{
    if(p==0)
        return;
    else
    {
        out(data[p].pre);
        if(data[p].sol==0)
            printf("FILL(%d)\n",data[p].s+1);
        else if(data[p].sol==1)
            printf("DROP(%d)\n",data[p].s+1);
        else if(data[p].sol==2)
            printf("POUR(%d,%d)\n",data[p].s+1,(data[p].s^1)+1);
    }
}

int main()
{
    while(~scanf("%d%d%d",&b[0],&b[1],&c))
    {

        if(c>b[0] && c>b[1])
        {
            printf("impossible\n");
            continue;
        }
        memset(vis,0,sizeof(vis));
        node k=bfs();
        if(k.t==-1)
            printf("impossible\n");
        else
        {
            printf("%d\n",k.t);
            out(k.id);
        }

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值