poj 3414 Pots bfs 记忆搜索

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 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 A, B, 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)

题意:

有两个杯子,容量分别为a, b, 有三种操作, 分别是1.清空当前杯子的水,2. 充满当前杯子的水, 3.将一个杯子中的水倒入另一个杯子
 求最短的步数, 并且输出操作步骤。

这题用bfs简单些, 只是多了个记忆路径, 可以用一个数组来储存路径。 

这题我做了两遍, 发现第二次反而做麻烦了, 水平越来越低了。

第一次做的代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
using namespace std;
int pota,potb,water;
typedef struct q
{
    int step;
    int x,y;
    int op;
    struct q * pre;
}q;
int vis[117][117];
queue <q> qq;
stack <int> ss;
int   bfs()
{
   int num=-1;
   q now[10000];
   q next;
   next.x=0; next.y=0;
   next.op=0; next.step=0;
   next.pre=NULL;
   vis[next.x][next.y]=1;
   qq.push(next);
   while (!qq.empty())
   {
       num++;
       now[num]=qq.front();
       qq.pop();
       for (int i=0;i<6;i++)
       {

           switch(i)
           {
               case 0:{next.x=pota;next.y=now[num].y;}  break;   //fill a;
               case 1:{next.y=potb;next.x=now[num].x;}  break;  //fill b;
               case 2:{
                         if(now[num].x>potb-now[num].y)
                        {
                           next.y=potb;
                           next.x=now[num].x-(potb-now[num].y);
                        }
                        else
                        {
                            next.y=now[num].x+now[num].y;
                            next.x=0;
                        }
                      }
                      break;                 //pour 1 to 2;
               case 3:{
                         if(now[num].y>pota-now[num].x)
                        {
                           next.x=pota;
                           next.y=now[num].y-(pota-now[num].x);
                        }
                        else
                        {
                            next.x=now[num].y+now[num].x;
                            next.y=0;
                        }         //pour 2 to 1;
                      }
                      break;
               case 4:{next.x=0;next.y=now[num].y;}   break;      //pour 1;
               case 5:{next.y=0;next.x=now[num].x;}   break;      //pour 2;
           }
           next.step=now[num].step+1;
           next.pre=&now[num];
           next.op=i;
           if(next.x==water||next.y==water)
           {
                int l=next.step;
              while (next.pre)
              {
                  ss.push(next.op);
                  next=*next.pre;
              }
              return l;
           }
           if(!vis[next.x][next.y])
           {
               vis[next.x][next.y]=1;
               qq.push(next);
           }
       }
   }
   return -1;
}
int main()
{
    scanf("%d%d%d",&pota,&potb,&water);
    memset (vis,0,sizeof(vis));
    int m=bfs();
        if(m==-1)
           printf("impossible\n");
        else
        {
            printf("%d\n",m);
            while (!ss.empty())
            {
               switch (ss.top())
               {
               case 0: printf("FILL(1)\n"); break;
               case 1: printf("FILL(2)\n"); break;
               case 2: printf("POUR(1,2)\n"); break;
               case 3: printf("POUR(2,1)\n"); break;
               case 4: printf("DROP(1)\n"); break;
               case 5: printf("DROP(2)\n"); break;
               }
               ss.pop();
            }
        }
    return 0;
}

第二次的代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int a,b,c;
int vis[105][105];
struct di
{
    int ope;
    int fro;
}; //存操作数;
int num=0;
di d[1000005];
int re[1000005];
struct road
{
    int capa;
    int capb;
    int no;   //在数组中的编号
    int step;
};
int bfs ()
{
    queue<road>q;
    road now,next;
    now.capa=0; now.capb=0;
    now.no=0; now.step=0;
    vis[0][0]=1;
    //d[num++]=-1;
    q.push(now);
    while (!q.empty())
    {
        now=q.front();
        q.pop();
        for (int i=0;i<6;i++)
        {
            if(i==0&&now.capa<a)
            {
                next.capa=a;
                next.capb=now.capb;
            }
            else if(i==1&&now.capb<b)
            {
                next.capb=b;
                next.capa=now.capa;
            }
            else if(i==2&&now.capa)
            {
                next.capa=0;
                next.capb=now.capb;
            }
            else if(i==3&&now.capb)
            {
                next.capb=0;
                next.capa=now.capa;
            }
            else if(i==4&&now.capa>0)
            {
                int temp=b-now.capb;
                if(now.capa>=temp)
                {
                    next.capb=b;
                    next.capa=now.capa-temp;
                }
               else
                {
                    next.capb=now.capb+now.capa;
                    next.capa=0;
                }
            }
            else if(i==5&&now.capb>0)
            {
                int temp=a-now.capa;
                if(now.capb>=temp)
                {
                    next.capa=a;
                    next.capb=now.capb-temp;
                }
                else
                {
                    next.capa=now.capa+now.capb;
                    next.capb=0;
                }
            }
            next.no=num;
            next.step=now.step+1;
            if(!vis[next.capa][next.capb])
            {
                d[num].ope=i;
                d[num++].fro=now.no;
                vis[next.capa][next.capb]=1;
                if(next.capa==c||next.capb==c)
               {
                return next.step;
               }
               q.push(next);
            }
        }
    }
    return -1;

}
int main()
{
    memset (vis,0,sizeof(vis));
    scanf("%d%d%d",&a,&b,&c);
    int step=bfs();
    if(step!=-1)
    {
        printf("%d\n",step);
        num--;
        for (int i=0;i<step;i++)
        {
            re[i]=d[num].ope;
            num=d[num].fro;
        }
        for (int i=step-1;i>=0;i--)
        {
            if(re[i]==0)
                printf("FILL(1)\n");
            else if(re[i]==1)
                printf("FILL(2)\n");
            else if(re[i]==2)
                printf("DROP(1)\n");
            else if(re[i]==3)
                printf("DROP(2)\n");
            else if(re[i]==4)
                printf("POUR(1,2)\n");
            else if(re[i]==5)
                printf("POUR(2,1)\n");

       }
    }
    else
        printf("impossible\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/81385905
今日推荐