bfs,dfs(回溯路徑)倒水

E - E
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int vis[110][110];
int a,b,c;
int step;
int flag;

struct node
{int k1,k2;
 int op;
 int step;
 int pre;

}q[110*110];
int id[110*110];
int lastindex;
void bfs()
{
    node now,next;
    int head,tail;
    head=tail=0;
    q[tail].k1=0;q[tail].k2=0;
    q[tail].op=0;q[tail].step=0;q[tail].pre=0;
    tail++;
    memset(vis,0,sizeof(vis));
    vis[0][0]=1;
    while(head<tail)
     {

      now=q[head];
        head++;
    if(now.k1==c||now.k2==c)
    {
        flag=1;
        step=now.step;
        lastindex=head-1;

    }
    for(int i=1;i<=6;i++)
    {
        if(i==1)
        {
            next.k1=a;
            next.k2=now.k2;
        }
        else if(i==2)
        {
            next.k1=now.k1;
            next.k2=b;
        }
        else if(i==3)
        {
            next.k1=0;
            next.k2=now.k2;

        }
        else if(i==4)
        {
            next.k1=now.k1;
            next.k2=0;
        }
        else if(i==5)
        {
            if(now.k1+now.k2<=b)
            {
                next.k1=0;
                next.k2=now.k1+now.k2;

            }
            else
            {
                next.k1=now.k1+now.k2-b;
                next.k2=b;
            }
        }
        else if(i==6)
        {
            if(now.k1+now.k2<=a)
            {
                next.k1=now.k1+now.k2;
                next.k2=0;

            }
            else
            {
                next.k1=a;
                next.k2=now.k1+now.k2-a;
            }
        }
        next.op=i;
        if(!vis[next.k1][next.k2])
        {
            vis[next.k1][next.k2]=1;
            next.step=now.step+1;
            next.pre=head-1;
            q[tail].k1=next.k1;q[tail].k2=next.k2;
            q[tail].op=next.op;q[tail].step=next.step;
            q[tail].pre=next.pre;
            tail++;
            if(next.k1==c||next.k2==c)
            {
                flag=1;
                step=next.step;
                lastindex=tail-1;
                return;
            }
        }
    }
}
}
int main()
{
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        flag=0;
        step=0;
        bfs();
        if(flag)
        {
            printf("%d\n",step);
            id[step]=lastindex;
            for(int i=step-1;i>=1;i--)
            {
                id[i]=q[id[i+1]].pre;
            }
            for(int i=1;i<=step;i++)
            {
                switch(q[id[i]].op)
                {
                    case 1:printf("FILL(1)\n");break;
                    case 2:printf("FILL(2)\n");break;
                    case 3:printf("DROP(1)\n");break;
                    case 4:printf("DROP(2)\n");break;
                    case 5:printf("POUR(1,2)\n");break;
                    case 6:printf("POUR(2,1)\n");break;
                }
            }
        }

            else
                printf("impossible\n");

        }
        return 0;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章