POJ Pots 3414 (bfs)

4 篇文章 0 订阅


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

题意:给你两种容量的罐子,让你经过几种操作,能否凑出指定容量的水

FILL(i)    把i罐子灌满

2.DROP(i)   把i罐子清空

3.POUR(i,j)  把i罐子的水倒进j罐子里去,如果不能全部倒进去,就剩到罐子里,能倒了,就清空了。


有6中入口,注意要标记状态(标记执行完的状态),用string存操作(网上看的,自己不会),存到每一个状态以前经过的操作,还有别的存法,还没学会

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <queue>
#include <iostream>
using namespace std;
int n,m,c;
struct node
{
    string s;
    int a,b;
    int step;
};
int v[111][111];
void bfs()
{
    memset(v,0,sizeof(v));
    queue<node>q;
    while(!q.empty())q.pop();
    struct node l,k;
    l.a=0;
    l.b=0;
    l.s+='0';
    l.step=0;
    q.push(l);
    int top=0;
    while(!q.empty())
    {
        k=q.front();
        q.pop();
        if(k.a==c||k.b==c)
        {
            printf("%d\n",k.step);
            struct node t;
            t=k;
            //cout<<k.s<<endl;
            for(int i=0;k.s[i]!='\0';i++)
            {
                if(k.s[i]=='1')
                {
                    printf("FILL(1)\n");
                }
                if(k.s[i]=='2')
                {
                    printf("FILL(2)\n");
                }
                if(k.s[i]=='3')
                {
                    printf("DROP(1)\n");
                }
                if(k.s[i]=='4')
                {
                    printf("DROP(2)\n");
                }
                if(k.s[i]=='5')
                {
                    printf("POUR(1,2)\n");
                }
                if(k.s[i]=='6')
                {
                    printf("POUR(2,1)\n");
                }


            }
            return ;
        }
        if(k.a<n)
        {
            struct node t;
            t.a=n;
            t.b=k.b;
            t.s=k.s+'1';//存操作
            t.step=k.step+1;
            if(!v[t.a][t.b])//标记状态
            {
                q.push(t);
                v[t.a][t.b]=1;
            }
        }
        if(k.b<m)
        {
            struct node t;
            t.a=k.a;
            t.b=m;
            t.s=k.s+'2';
            t.step=k.step+1;
            if(!v[t.a][t.b])
            {
                q.push(t);
                v[t.a][t.b]=1;
            }
        }
        if(k.a!=0)
        {
            struct node t;
            t.a=0;
            t.b=k.b;
            t.s=k.s+'3';
            t.step=k.step+1;
            if(!v[t.a][t.b])
            {
                q.push(t);
                v[t.a][t.b]=1;
            }
        }
        if(k.b!=0)
        {
            struct node t;
            t.a=k.a;
            t.b=0;
            t.s=k.s+'4';
            t.step=k.step+1;
            if(!v[t.a][t.b])
            {
                q.push(t);
                v[t.a][t.b]=1;
            }
        }
        if(k.a!=0 && k.a<=n && k.b<m)
        {
            struct node t;
            int g=m-k.b;
            if(g<k.a)
            {
                t.a=k.a-g;
            }
            else
            {
                t.a=0;
            }
            if(g>k.a)
            {
                t.b=k.b+k.a;
            }
            else
            {
                t.b=m;
            }
            t.s=k.s+'5';
            t.step=k.step+1;
            if(!v[t.a][t.b])
            {
                q.push(t);
                v[t.a][t.b]=1;
            }
        }
        if(k.b!=0 && k.b<=m && k.a<n)
        {
            struct node t;
            int g=n-k.a;
            if(g<k.b)
            {
                t.b=k.b-g;
            }
            else
            {
                t.b=0;
            }
            if(g>k.b)
            {
                t.a=k.b+k.a;
            }
            else
            {
                t.a=n;
            }
            t.s=k.s+'6';
            t.step=k.step+1;
           if(!v[t.a][t.b])
            {
                q.push(t);
                v[t.a][t.b]=1;
            }
        }


    }
    printf("impossible\n");
    return ;
}
int main()
{
    scanf("%d%d%d",&n,&m,&c);
    bfs();
    return 0;



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值