pots题解(BFS简单应用)

题目
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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,题目中每一个操作都代表每一步(1:向A倒满水,2:向B倒满水,3:把A的水倒空,4:把B的水倒空,5:A向B倒水[可能倒完,也可能没倒完],6:B向A倒水[可能倒完,也可能没倒完]),编写一个程序找出最短路径使A或B中的水的体积为C,如果不存在则输出impossible。

利用回溯输出
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;
}
}
通过函数的递归回溯,因为当找到了最短路径后,存储的是终点,因为所走的每一步都记录了上一步的路径,可以通过自身的调用来从起点输出。

具体实现

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <queue>
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>q;

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){
        q.push(d);
        lu[d.a][d.b]=d;
        f[d.a][d.b]=1;
    }
}

void bfs(){
    q.push(lu[0][0]);
    while(!q.empty()){
        node u=q.front();
        q.pop();
        if(u.a==C || u.b==C ){
            printf("%d\n",u.step);
            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加满
            v.a=A;
            v.b=u.b;
            v.way=1;
            push(v);
        }

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

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

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

        if(u.a!=0 && u.b!=B){  //把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.a!=A && u.b!=0){   //把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);
        }



    }
    printf("impossible\n");
}

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;
    scanf("%d %d %d",&A,&B,&C);
    bfs();
    return 0;
}
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值