poj 3414(搜索)

来源:互联网 发布:excel数据列表创建列表 编辑:程序博客网 时间:2024/06/06 09:34

                                                                                                                                                Pots
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15105 Accepted: 6354 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 ≤ i ≤ 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 potj is full (and there may be some water left in the pot i), or the poti 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 exactlyC liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, andC. 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 operationsK. 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

6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)

题意:给出两个容积分别为 a 和 b的pot,按照以下三种操作方式,求出能否在一定步数后,使者两个pot的其中一个的水量为c。

     1.FILL(i):将ipot倒满水。

      2.DROP(i):将ipot倒空水。

     3.POUR(i,j): 将ipot的水倒到jpot上,直至要么ipot为空,要么jpot为满。


#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;struct node{    int va,vb,step;    char str[151][151];};int a,b,c,vis[151][151];int bfs(){    node q[151],s,t;    int i,j;    s.va=0;    s.vb=0;    s.step=0;    int r,f;    vis[0][0]=1;    r=f=0;    q[r++]=s;    while(f<r)    {        s=q[f++];        if(s.va==c||s.vb==c)        {            printf("%d\n",s.step);            for(int i=0; i<s.step; i++)            {                printf("%s\n",s.str[i]);            }            return 1;        }        //装满a        if(s.va==0)        {            t=s;            t.va=a;            strcpy(t.str[t.step++],"FILL(1)");            if(!vis[t.va][t.vb])            {                vis[t.va][t.vb]=1;                q[r++]=t;            }        }        else if(s.va<=a)        {            t=s;            t.va=0;            strcpy(t.str[t.step++],"DROP(1)");            if(!vis[t.va][t.vb])            {                vis[t.va][t.vb]=1;                q[r++]=t;            }            if(s.vb<b)            {                t=s;                if(t.vb+t.va<=b)                {                    t.vb+=t.va;                    t.va=0;                }                else                {                    t.va=(t.vb+t.va)-b;                    t.vb=b;                }                strcpy(t.str[t.step++],"POUR(1,2)");                if(!vis[t.va][t.vb])                {                    vis[t.va][t.vb]=1;                    q[r++]=t;                }            }        }        //装满b        if(s.vb==0)        {            t=s;            t.vb=b;            strcpy(t.str[t.step++],"FILL(2)");            if(!vis[t.va][t.vb])            {                vis[t.va][t.vb]=1;                q[r++]=t;            }        }        //把b中的水倒出来        else if(s.vb<=b)        {            t=s;            t.vb=0;            strcpy(t.str[t.step++],"DROP(2)");            if(!vis[t.va][t.vb])            {                vis[t.va][t.vb]=1;                q[r++]=t;            }            if(s.va<a)            {                t=s;                if(t.vb+t.va<=a)                {                    t.va+=t.vb;                    t.vb=0;                }                else                {                    t.vb=(t.vb+t.va)-a;                    t.va=a;                }                strcpy(t.str[t.step++],"POUR(2,1)");                if(!vis[t.va][t.vb])                {                    vis[t.va][t.vb]=1;                    q[r++]=t;                }            }        }    }    return 0;}int main(){    while(~scanf("%d%d%d",&a,&b,&c))    {        memset(vis,0,sizeof(vis));        int f=bfs();        if(!f)            printf("impossible\n");    }    return 0;}



0 0
原创粉丝点击