POJ 3414 Pots

POJ 3414 Pots

题目描述
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,开始两个壶都是空的,然后你可以把一个壶倒满,或者倒空,或者把一个壶里的水到给另一个壶直到另一个壶被倒满或者这个壶被倒空。 问你通过一系列的操作后是否可以使得其中一个壶里水的量为C,如果可以则输出最少操作次数,并输出相应的步骤,否则输出impossible.

思路
利用队列用bfs求最优解。将每次的操作和当前两个壶里的水保存在二维数组里,然后利用回溯输出结果。

下面是代码

#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<sstream>
#include<ctime>
using namespace std;  

int f[105][105];    //用来标记当前状态是否已经有过
int A,B,C;
struct node   //定义结构体
{
    int a,b;//两个容器中的水 
    int sa,sb;//上个状态 
    int way;  //  操作1到6 
    int step;  // 当前步数 
}lu[105][105];   //用来保存操作

queue<node> que;  

void print(int a,int b)     //利用回溯输出
{
    if(lu[a][b].a==0&&lu[a][b].b==0) //到达初始状态结束
    return ;
    print(lu[a][b].sa,lu[a][b].sb);
    switch(lu[a][b].way)
    {
        case 1: cout<<"FILL(1)"<<endl;break;
        case 2: cout<<"FILL(2)"<<endl;break;
        case 3: cout<<"DROP(1)"<<endl;break;
        case 4: cout<<"DROP(2)"<<endl;break;
        case 5: cout<<"POUR(1,2)"<<endl;break;
        case 6: cout<<"POUR(2,1)"<<endl;break;
    }
}

void push(node d) //入队操作
{
    if(f[d.a][d.b]==0) 
    {
        que.push(d);
        lu[d.a][d.b]=d;
        f[d.a][d.b]=1;
    }
}
void bfs()  
{  
     /*while(!que.empty())  
      que.pop();      */   
      que.push(lu[0][0]);   //将第一个点入队
      while(!que.empty())   //队列非空
      {
        node u=que.front();  //取队首元素
        que.pop();           //将队首元素出队
        if(u.a==C||u.b==C)      //如果两个壶中有一个满足条件则输出,结束bfs
        {
            cout<<u.step<<endl;
            print(u.a,u.b);
            return;
          }

          node v;        //辅助点
          v.sa=u.a;      //赋值
          v.sb=u.b;
          v.step=u.step+1;
          if(u.a!=A)  //1. 加满1 
          {
            v.a=A;
            v.b=u.b;
            v.way=1;
            push(v);
          }

          if(u.b!=B) //2. 加满2
          {
            v.a=u.a;
            v.b=B;
            v.way=2;
            push(v);
          }

          if(u.a!=0) //3.倒光1 
          {
            v.a=0;
            v.b=u.b;
            v.way=3;
            push(v);
          } 

          if(u.b!=0) //4.倒光2 
          {
            v.a=u.a;
            v.b=0;
            v.way=4;
            push(v);
           } 

           if(u.a!=0&&u.b!=B)//5. 把1到给2 
           {
                if(u.a+u.b>=B)
                {
                    v.b=B;
                    v.a=u.a+u.b-B;
                   }

                 else 
                 {
                    v.a=0;
                    v.b=u.a+u.b;
                   }
                 v.way=5;
                 push(v);    
            } 
            if(u.b!=0&&u.a!=A)//6. 把2到给1 
            {
                if(u.a+u.b>=A)
                {
                    v.a=A;
                    v.b=u.a+u.b-A;
                }
                else
                {
                    v.a=u.a+u.b;
                    v.b=0;
                }
                v.way=6;
                push(v);
            }
      }
      cout<<"impossible"<<endl;
}  

int main()  
{  
      memset(f,0,sizeof(f));
      lu[0][0].a=lu[0][0].b=0;
      lu[0][0].sa=lu[0][0].sb=-1;
      lu[0][0].step=0;
      f[0][0]=1;
      cin>>A>>B>>C;
      bfs();
    return 0;  
}  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值