POJ 3414 Pots(BFS)

题目链接:http://poj.org/problem?id=3414

题目大意:给定容量a和b的杯子,6种操作,问能否倒得c容量的水,不能输出impossible,否则要求步数最小

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

思路:bfs每一步,第一次到a为x,b为y容量的操作次数最短,用数组s记录每一步操作,用prex和prey记录父节点,最后递归输出

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;

int a,b,c,ansa,ansb;
int vis[105][105];
int prex[105][105];
int prey[105][105];
char s[105][105][10];

struct node
{
    int x,y;
    int step;
};

void output(int a,int b)
{
    if(a==0&&b==0)
        return ;
    else
    {
        output(prex[a][b],prey[a][b]);
        printf("%s\n",s[a][b]);
    }
}

int bfs()
{
    MEM(vis,0);
    node now,next;
    now.x=0;
    now.y=0;
    now.step=0;
    vis[now.x][now.y]=1;
    queue<node>q;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.x==c||now.y==c)
        {
            ansa=now.x;
            ansb=now.y;
            return now.step;
        }
        //倒满1
        next.x=a;
        next.y=now.y;
        next.step=now.step+1;
        if(vis[next.x][next.y]==0)
        {
            vis[next.x][next.y]=1;
            strcpy(s[next.x][next.y],"FILL(1)");
            prex[next.x][next.y]=now.x;
            prey[next.x][next.y]=now.y;
            q.push(next);
        }

        //倒满2
        next.x=now.x;
        next.y=b;
        next.step=now.step+1;
        if(vis[next.x][next.y]==0)
        {
            vis[next.x][next.y]=1;
            strcpy(s[next.x][next.y],"FILL(2)");
            prex[next.x][next.y]=now.x;
            prey[next.x][next.y]=now.y;
            q.push(next);
        }

        //倒掉1
        next.x=0;
        next.y=now.y;
        next.step=now.step+1;
        if(vis[next.x][next.y]==0)
        {
            vis[next.x][next.y]=1;
            strcpy(s[next.x][next.y],"DROP(1)");
            prex[next.x][next.y]=now.x;
            prey[next.x][next.y]=now.y;
            q.push(next);
        }

        //倒掉2
        next.x=now.x;
        next.y=0;
        next.step=now.step+1;
        if(vis[next.x][next.y]==0)
        {
            vis[next.x][next.y]=1;
            strcpy(s[next.x][next.y],"DROP(2)");
            prex[next.x][next.y]=now.x;
            prey[next.x][next.y]=now.y;
            q.push(next);
        }

        //1往2倒
        if(now.x>=b-now.y)
        {
            next.x=now.x-(b-now.y);
            next.y=b;
        }
        else
        {
            next.x=0;
            next.y=now.x+now.y;
        }
        next.step=now.step+1;
        if(vis[next.x][next.y]==0)
        {
            vis[next.x][next.y]=1;
            strcpy(s[next.x][next.y],"POUR(1,2)");
            prex[next.x][next.y]=now.x;
            prey[next.x][next.y]=now.y;
            q.push(next);
        }

        //2往1倒
        if(now.y>=a-now.x)
        {
            next.y=now.y-(a-now.x);
            next.x=a;
        }
        else
        {
            next.x=now.x+now.y;
            next.y=0;
        }
        next.step=now.step+1;
        if(vis[next.x][next.y]==0)
        {
            vis[next.x][next.y]=1;
            strcpy(s[next.x][next.y],"POUR(2,1)");
            prex[next.x][next.y]=now.x;
            prey[next.x][next.y]=now.y;
            q.push(next);
        }

    }
    return -1;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        int ans=bfs();
        if(ans==-1)
            printf("impossible\n");
        else
        {
            printf("%d\n",ans);
            output(ansa,ansb);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值