Pots(DFS &BFS)

//新生训练

cpp 复制代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 205;
int n, m;
int l;
int A, B, C;
int dis[N][N];

struct node
{
    int px, py, op;
};

node pre[N][N];

void dfs(int x, int y)
{
    if (x == 0 && y == 0)
    {
        return;
    }
    dfs(pre[x][y].px, pre[x][y].py);
    switch (pre[x][y].op)
    {
    case 1:
        cout << "FILL(1)\n";
        break;
    case 2:
        cout << "FILL(2)\n";
        break;
    case 3:
        cout << "DROP(1)\n";
        break;
    case 4:
        cout << "DROP(2)\n";
        break;
    case 5:
        cout << "POUR(1,2)\n";
        break;
    case 6:
        cout << "POUR(2,1)\n";
    }
}

bool flag = 1;

void bfs()
{
    queue<PII> q;
    q.push(make_pair(0, 0));
    dis[0][0] = 0;
    while (!q.empty())
    {
        PII t = q.front();
        q.pop();

        int x = t.first, y = t.second;
        if (x == C || y == C)
        {
            cout << dis[x][y] << '\n';
            dfs(x, y);
            flag = 0;
            return;
        }
        int ix, iy;

        // FILL(1)
        ix = A, iy = y;
        if (dis[ix][iy] == -1)
        {
            dis[ix][iy] = dis[x][y] + 1;
            pre[ix][iy] = {x, y, 1};
            q.push(make_pair(ix, iy));
        }

        // FILL(2)
        ix = x, iy = B;
        if (dis[ix][iy] == -1)
        {
            dis[ix][iy] = dis[x][y] + 1;
            pre[ix][iy] = {x, y, 2};
            q.push(make_pair(ix, iy));
        }

        // DROP(1)
        ix = 0, iy = y;
        if (dis[ix][iy] == -1)
        {
            dis[ix][iy] = dis[x][y] + 1;
            pre[ix][iy] = {x, y, 3};
            q.push(make_pair(ix, iy));
        }

        // DROP(2)
        ix = x, iy = 0;
        if (dis[ix][iy] == -1)
        {
            dis[ix][iy] = dis[x][y] + 1;
            pre[ix][iy] = {x, y, 4};
            q.push(make_pair(ix, iy));
        }

        // POUR(1,2)
        ix = x, iy = y;
        while (ix && iy < B)
        {
            --ix;
            ++iy;
        }
        if (dis[ix][iy] == -1)
        {
            dis[ix][iy] = dis[x][y] + 1;
            pre[ix][iy] = {x, y, 5};
            q.push(make_pair(ix, iy));
        }

        // POUR(2,1)
        ix = x, iy = y;
        while (iy && ix < A)
        {
            ++ix;
            --iy;
        }
        if (dis[ix][iy] == -1)
        {
            dis[ix][iy] = dis[x][y] + 1;
            pre[ix][iy] = {x, y, 6};
            q.push(make_pair(ix, iy));
        }
    }
}

void solve()
{
    memset(dis, -1, sizeof dis);
    cin >> A >> B >> C;
    bfs();
    if (flag)
        cout << "impossible";
}

signed main()
{
    int t = 1;
    // cin>>t;
    while (t--)
    {
        solve();
    }
    return 0;
}

//感谢学长的讲解 hwh ;

~~~//仅当笔者个人备忘录使用。

相关推荐
爱写代码的刚子1 分钟前
C++知识总结
java·开发语言·c++
s_little_monster29 分钟前
【QT】QT入门
数据库·c++·经验分享·笔记·qt·学习·mfc
Yingye Zhu(HPXXZYY)36 分钟前
洛谷 P11045 [蓝桥杯 2024 省 Java B] 最优分组
c++·蓝桥杯
三玖诶1 小时前
第一弹:C++ 的基本知识概述
开发语言·c++
睡不着还睡不醒1 小时前
【数据结构强化】应用题打卡
算法
sp_fyf_20241 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-05
人工智能·深度学习·神经网络·算法·机器学习·语言模型·自然语言处理
C++忠实粉丝2 小时前
前缀和(6)_和可被k整除的子数组_蓝桥杯
算法
木向2 小时前
leetcode42:接雨水
开发语言·c++·算法·leetcode
TU^2 小时前
C语言习题~day16
c语言·前端·算法
sukalot2 小时前
windows C++-创建基于代理的应用程序(下)
c++