POJ3414BFS+路径记录

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

        题意:给出两个无刻度的瓶子,对于每个瓶子有三种操作,倒掉瓶子里面的水、注满水、将瓶子里面的水倒入另一个瓶子中,多出来的水留在该瓶子中。求得到给定的水最少步数,并输出操作。

        解题思路:这个题是个典型的BFS题,用两个变量模拟瓶子中的水数,然后对于每个瓶子的三种操作写出来,用flag数组标记已经进行过的状态,最后得到给定的水。前面的很简单,主要是记录路径。因为我们用BFS查找肯定有不同的分支,冒然的用数字存肯定会把左右的查找过的点全部存下。我们找到的最后一个节点,从他到起点是最短的路无疑,那么对于这条路上的所有节点到起点都是最短,那么我们存储的时候只需要把此节点的操作和上一个节点的下标存入即可,最后可以用递归或者栈输出。我这里用的栈。以下是我的AC代码

#include <cstdio>
#include <iostream>
using namespace std;
#include <queue>
#include <stack>
#include <memory.h>
#define maxn 105

int flag[maxn][maxn];
int a,b,n;
struct trip
{
    int x,y,step;
    int pre;
    int id;
    int cz;
} t[maxn];
void bfs()
{
    memset(flag,0,sizeof(flag));
    queue<trip> q;
    stack<int> s;
    trip now,next;
    t[0].pre=-1;
    int cnt=0;
    now.x=0;
    now.y=0;
    now.step=0;
    now.pre=-1;
    now.id=0;
    now.cz=0;
    flag[now.x][now.y]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.x==n||now.y==n)
        {
            printf("%d\n",now.step);
            int z=now.id;
            while(t[z].pre!= -1)
            {
                s.push(t[z].cz);
                z=t[z].pre;
            }
            for(int i=0;i<now.step;i++)
            {
                int a = s.top();
                if(a==1) printf("FILL(1)");
                else if(a==2) printf("DROP(1)");
                else if(a==3) printf("POUR(1,2)");
                else if(a==4) printf("FILL(2)");
                else if(a==5) printf("DROP(2)");
                else if(a==6) printf("POUR(2,1)");
                s.pop();
                printf("\n");
            }
            return;
        }
        if(now.x)
        {
            next=now;
            next.x=a;// 注满1号杯
            if(!flag[next.x][next.y])
            {
                next.step=now.step+1;
                flag[next.x][next.y]=1;
                next.id=++cnt;
                t[next.id].cz=1;
                t[next.id].pre=now.id;
                q.push(next);
            }
            next=now;
            next.x=0;// 清空1号杯
            if(!flag[next.x][next.y])
            {
                next.step=now.step+1;
                flag[next.x][next.y]=1;
                next.id=++cnt;
                t[next.id].cz=2;
                t[next.id].pre=now.id;
                q.push(next);
            }
            next=now;
            next.x=now.x-(min(b,now.y+now.x)-now.y);
            next.y=min(b,now.y+now.x);  //1倒入2
            if(!flag[next.x][next.y])
            {
                next.step=now.step+1;
                flag[next.x][next.y]=1;
                next.id=++cnt;
                t[next.id].cz=3;
                t[next.id].pre=now.id;
                q.push(next);
            }
        }
        else
        {
            next=now;
            next.x=a;//注满1号杯
            if(!flag[next.x][next.y])
            {
                next.step=now.step+1;
                flag[next.x][next.y]=1;
                next.id=++cnt;
                t[next.id].cz=1;
                t[next.id].pre=now.id;
                q.push(next);
            }
        }
        if(now.y)
        {
            next=now;
            next.y=b;//注满2号杯
            if(!flag[next.x][next.y])
            {
                next.step=now.step+1;
                flag[next.x][next.y]=1;
                next.id=++cnt;
                t[next.id].cz=4;
                t[next.id].pre=now.id;
                q.push(next);
            }
            next=now;
            next.y=0;//清空2号杯
            if(!flag[next.x][next.y])
            {
                next.step=now.step+1;
                flag[next.x][next.y]=1;
                next.id=++cnt;
                t[next.id].cz=5;
                t[next.id].pre=now.id;
                q.push(next);
            }
            next=now;
            next.y=now.y-(min(a,now.y+now.x)-now.x);
            next.x=min(a,now.y+now.x);//2倒入1
            if(!flag[next.x][next.y])
            {
                next.step=now.step+1;
                flag[next.x][next.y]=1;
                next.id=++cnt;
                t[next.id].cz=6;
                t[next.id].pre=now.id;
                q.push(next);
            }
        }
        else
        {
            next=now;
            next.y=b; //注满2号杯
            if(!flag[next.x][next.y])
            {
                next.step=now.step+1;
                flag[next.x][next.y]=1;
                next.id=++cnt;
                t[next.id].cz=4;
                t[next.id].pre=now.id;
                q.push(next);
            }
        }
    }
    printf("impossible\n");
}
int main()
{
    scanf("%d%d%d",&a,&b,&n);
    bfs();
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值