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 ;

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

相关推荐
hutaotaotao29 分钟前
c++中的输入输出流(标准IO,文件IO,字符串IO)
c++·io·fstream·sstream·iostream
机器学习之心30 分钟前
机器学习用于算法交易(Matlab实现)
算法·机器学习·matlab
AL流云。31 分钟前
【优选算法】C++滑动窗口
数据结构·c++·算法
qq_429879672 小时前
省略号和可变参数模板
开发语言·c++·算法
CodeWithMe3 小时前
【C/C++】std::vector成员函数清单
开发语言·c++
uyeonashi3 小时前
【QT控件】输入类控件详解
开发语言·c++·qt
飞川撸码3 小时前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
Neil今天也要学习3 小时前
永磁同步电机参数辨识算法--IPMSM拓展卡尔曼滤波全参数辨识
单片机·嵌入式硬件·算法
yzx9910134 小时前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
亮亮爱刷题4 小时前
算法练习-回溯
算法