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

he 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’.

这题的意思是输入三个数字,分别代表A瓶的容积,B瓶的容积,想要获得的体积,然后可以进行题目中的4种操作,求最短的过程。

这题的意思很明确,就是用BFS,一得到想要获得的体积,就回溯输出过程。因为是广搜,所以求到即最短。



#include <iostream
using namespace std;

int a, b,c1,x,y,work,c;
int mark[120][120] = { 0 };//用来记录是否达到过这种状态
 
struct node
{
    int x, y;//记录A,B瓶装水的情况
    int work;//记录操作
    int c; //记录上一步,输出时用来递归
}queue[5000];
 
void print( int head)
{
    static int i=-1;//记录次数
    i++;
    if (queue[head].c != -1)
    {
         print(queue[head].c);
         if (queue[head].work == 1)
             cout<< "FILL(1)" << endl;
         if (queue[head].work == 2)
             cout<< "FILL(2)" << endl;
         if (queue[head].work == 3)
             cout<< "DROP(1)" << endl;
         if (queue[head].work == 4)
             cout<< "DROP(2)" << endl;
         if (queue[head].work == 5)
             cout<< "POUR(1,2)" << endl;
         if (queue[head].work == 6)
             cout<< "POUR(2,1)" << endl;
         return ;
                  
    }
    cout<< i << endl;
}

 

void bfs()
{
    int head = 0, tail = 1;
    queue[0].x= 0;
    queue[0].y= 0;
    queue[0].work= 0;
    queue[0].c= -1;
    while (head < tail)
    {
         if (queue[head].x ==c1 || queue[head].y == c1)//如果达到目标,输出
         {
             print(head);
             return;
         }
         for (int i = 1; i <= 6;i++)
         {
             if (i == 1)//倒满a杯
             {
                  x= a;
                  y= queue[head].y;
                  work= 1;
                  c= head;
                  if (mark[x][y] == 0)//判断是否经历过这种状态,如果没有,入列
                  {
                      mark[x][y]= 1;
                      queue[tail].x= x;
                      queue[tail].y= y;
                      queue[tail].c= c;
                      queue[tail].work= work;
                      tail++;
                  }
             }
             if (i == 2)//倒满B杯
             {
                  x= queue[head].x;
                  y= b;
                  work= 2;
                  c= head;
                  if (mark[x][y] == 0)
                  {
                      mark[x][y]= 1;
                      queue[tail].x= x;
                      queue[tail].y= y;
                      queue[tail].c= c;
                      queue[tail].work= work;;
                      tail++;
                  }
             }
             if (i == 3)//倒掉A杯
             {
                  x= 0;
                  y= queue[head].y;
                  work= 3;
                  c= head;
                  if (mark[x][y] == 0)
                  {

                      mark[x][y]= 1;
                      queue[tail].x= x;
                      queue[tail].y= y;
                      queue[tail].c= c;
                      queue[tail].work= work;
                      tail++;
                  }
             }
             if (i == 4)//倒掉A杯
             {
                  x= queue[head].x;
                  y= 0;
                  work= 4;
                  c= head;
                  if (mark[x][y] == 0)
                  {
                      mark[x][y]= 1;
                      queue[tail].x= x;
                      queue[tail].y= y;
                      queue[tail].c= c;
                      queue[tail].work= work;
                      tail++;
                  }
             }
             if (i == 5)//将A杯中的水倒进B杯
             {
                  if (queue[head].x >b - queue[head].y)//判断A杯中的水是否能倒满B杯
                  {
                      x= queue[head].x - (b - queue[head].y);
                      y= b;
                      
                  }
                  else 
                  {
                      x= 0;
                      y= queue[head].y + queue[head].x;
                  }
                    work = 5;
                      c= head;
                      if (mark[x][y] == 0)
                      {
                          mark[x][y]= 1;
                          queue[tail].x= x;
                          queue[tail].y= y;
                          queue[tail].c= c;
                          queue[tail].work= work;
                          tail++;
                      }
                  
             }
             if (i == 6)//将B杯中的水倒进A杯
             {
                  if (queue[head].y >a - queue[head].x)
                  {
                      y= queue[head].y - (a - queue[head].x);
                      x= a;                
                  }
                  else
                  {
                      y= 0;
                      x= queue[head].x + queue[head].y;
                  }
                  work= 6;
                  c= head;
                  if (mark[x][y] == 0)
                  {
                      mark[x][y]= 1;
                      queue[tail].x= x;
                      queue[tail].y= y;
                      queue[tail].c= c;
                      queue[tail].work= work;
                      tail++;
                  }

             }
           }
         head++;
    }
    cout<< "impossible" << endl;
}
 
  
int main()
{
    cin>> a >> b >> c1;
    bfs();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值