POJ-3414 pots

该博客介绍了一种使用广度优先搜索(BFS)算法来解决经典水壶倒水问题的方法。通过创建状态节点并进行剪枝避免重复状态,最终找到达到目标水量的最短操作序列。博客中包含详细的代码实现,并展示了如何输出最小操作步骤及对应的指令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

常规思路:

1.想清楚六种情况,大致想出代码

2.不要忘记剪枝(去除重复状态)

3.对于无解情况加入一个bool判断

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int N = 100010;
typedef struct
{
    int v1;
    int v2;
    string op;
} f;
f q[N];
int v1, v2, goal;
int head, tail;
void bfs();
bool p = false;
bool a[105][105] = {false};
int min_length = 0x7fffffff;
string short_str;
int main()
{
    cin >> v1 >> v2 >> goal;
    bfs();
    if (p)
    {
        cout << min_length << endl;
        for (int i = 0; i < short_str.length(); i++)
        {
            if (short_str[i] == '1')
            {
                printf("FILL(1)\n");
            }
            else if (short_str[i] == '2')
            {
                printf("DROP(1)\n");
            }
            else if (short_str[i] == '3')
            {
                printf("POUR(1,2)\n");
            }
            else if (short_str[i] == '4')
            {
                printf("FILL(2)\n");
            }
            else if (short_str[i] == '5')
            {
                printf("DROP(2)\n");
            }
            else if (short_str[i] == '6')
            {
                printf("POUR(2,1)\n");
            }
        }
    }
    else
        printf("impossible");
    system("pause");
    return 0;
}

void bfs()
{
    f init = {0, 0, ""};
    head = tail = 0;
    f nwq, cq;
    q[head] = init;
    while (head <= tail)
    {
        cq = q[head++];
        for (int i = 1; i <= 6; i++)
        {
            nwq = cq;
            if (i == 1)
            {
                nwq.v1 = v1;
                nwq.op += "1";
            }
            else if (i == 2)
            {
                nwq.v1 = 0;
                nwq.op += "2";
            }
            else if (i == 3)
            {
                int d = v2 - nwq.v2;
                if (d > nwq.v1)
                {
                    nwq.v2 += nwq.v1;
                    nwq.v1 = 0;
                }
                else
                {
                    nwq.v2 = v2;
                    nwq.v1 -= d;
                }
                nwq.op += "3";
            }
            else if (i == 4)
            {
                nwq.v2 = v2;
                nwq.op += "4";
            }
            else if (i == 5)
            {
                nwq.v2 = 0;
                nwq.op += "5";
            }
            else if (i == 6)
            {
                int d = v1 - nwq.v1;
                if (d > nwq.v2)
                {
                    nwq.v1 += nwq.v2;
                    nwq.v2 = 0;
                }
                else
                {
                    nwq.v1 = v1;
                    nwq.v2 -= d;
                }
                nwq.op += "6";
            }
            if (a[nwq.v1][nwq.v2])
                continue;
            else
            {
                a[nwq.v1][nwq.v2] = true;
                if (nwq.v1 == goal || nwq.v2 == goal)
                {
                    if (nwq.op.length() < min_length)
                    {
                        min_length = nwq.op.length();
                        short_str = nwq.op;
                        p = true;
                    }
                }
                q[++tail] = nwq;
            }
        }
    }
    return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值