poj 3414 Pots——bfs

Pots

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 ≤ 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。

  1. FILL(i):将i号杯子倒满
  2. DORP(i):将i号杯子的水全部倒掉
  3. POUR(i,j):将i号杯子的水倒入j号杯

 
题解: 利用dfs分情况入队:

  1. 倒满1号杯,将此时两个杯子中盛水的状态入队
  2. 倒满2号杯,入队
  3. 倒空1号杯,入队
  4. 倒空2号杯,入队
  5. 将1号杯倒入2号杯,入队
  6. 将2号杯倒入1号杯,入队

详细逻辑见代码:

c++ AC 代码

#include<cstdio>
#include<queue>
#include<cstring>
#include<cstdlib>
#define size 200
using namespace std;

int vis[size][size];    // 代表1号杯和2号杯的状态一维坐标代表1号杯状态,二维代表2号杯状态
int A, B, C;

struct node
{
    int v1; // 1号杯子中的水的体积
    int v2; // 2号杯子中的水的体积
    int pre_v1;     // 上个状态1号杯子的水的体积
    int pre_v2;     // 上个状态2号杯子的水的体积
    int way;        // 操作1~6
    int step;       // 当前步数
}dp_ab[size][size];
queue<node> que;

void bfs();
void print(int ,int);

int main()
{
    memset(vis,0,sizeof(vis));  // 初始化所有状态
    dp_ab[0][0].v1 = dp_ab[0][0].v2 = 0;    // 刚开始两个杯子都是空的
    dp_ab[0][0].pre_v1 = dp_ab[0][0].pre_v2 = -1;   // 他们前面没有状态,他们本身就是初始状态
    dp_ab[0][0].step = 0;       // 还没有进行操作,初始化为0
    scanf("%d%d%d",&A,&B,&C);
    bfs();
    // system("pause");
    return 0;
}

void print(int a,int b)
{
    if(dp_ab[a][b].v1 == 0 && dp_ab[a][b].v2 == 0) // 达到初始状态
        return;
    print(dp_ab[a][b].pre_v1,dp_ab[a][b].pre_v2);
    switch (dp_ab[a][b].way)
    {
    case 1: puts("FILL(1)");break;
    case 2: puts("FILL(2)");break;
    case 3:puts("DROP(1)");break;
    case 4:puts("DROP(2)");break;
    case 5:puts("POUR(1,2)");break;
    case 6:puts("POUR(2,1)");break;
    default:
        break;
    }
}

void push(node d)
{
    if(!vis[d.v1][d.v2])  // 如果之前没有存在过这个状态,把这个状态的1号杯和2号杯进队
    {
        que.push(d);
        vis[d.v1][d.v2] = 1;        // 标记这个状态已经存在
        dp_ab[d.v1][d.v2] = d;      // 将该节点存放到1、2号杯体积相对应的数组节点中
    }
}

void bfs()
{
    que.push(dp_ab[0][0]);
    while(!que.empty())
    {
        node pre_node = que.front();
        que.pop();
        if(pre_node.v1 == C || pre_node.v2 == C)
        {
            printf("%d\n",pre_node.step);
            print(pre_node.v1,pre_node.v2);
            return;
        }

        node now;
        now.pre_v1 = pre_node.v1;
        now.pre_v2 = pre_node.v2;
        now.step = pre_node.step + 1;

        if(pre_node.v1 != A)   // 把1加满
        {
            now.v1 = A;
            now.v2 = pre_node.v2;   // 加满1,但是2的状态不变,所以还是继承原来的状态
            now.way = 1;    // 操作为1号操作
            push(now);
        }

        if(pre_node.v2 != B)  // 把2加满
        {
            now.v2 = B;
            now.v1 = pre_node.v1;
            now.way = 2;
            push(now);
        }

        if(pre_node.v1 != 0)    // 把1号杯倒掉
        {
            now.v1 = 0;
            now.v2 = pre_node.v2;
            now.way = 3;
            push(now);
        }

        if(pre_node.v2 != 0)  // 把2倒光
        {
            now.v2 = 0;
            now.v1 = pre_node.v1;
            now.way = 4;
            push(now);
        }

        if(pre_node.v1 && pre_node.v2 != B)  // 把1倒给2
        {
            if(pre_node.v1+pre_node.v2 >= B)
            {
                now.v2 = B;
                now.v1 = pre_node.v1 + pre_node.v2 - B;
            }
            else
            {
                now.v2 = pre_node.v1 + pre_node.v2;
                now.v1 = 0;
            }
            now.way = 5;
            push(now);
        }

        if(pre_node.v1 != A && pre_node.v2)     // 把2倒给1
        {
            if(pre_node.v1 + pre_node.v2 >= A)
            {
                now.v1 = A;
                now.v2 = pre_node.v1 + pre_node.v2 - A;
            }
            else
            {
                now.v1 = pre_node.v1 + pre_node.v2;
                now.v2 = 0;
            }
            now.way = 6;
            push(now);
        }
    }
    puts("impossible");
}

代码参考自:https://blog.csdn.net/qq_43301061/article/details/85261352

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值