Pots(DFS &BFS)

//新生训练

#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. TCP协议是安全的吗?

    2024-04-04 08:02:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-04 08:02:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-04 08:02:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-04 08:02:02       18 阅读

热门阅读

  1. 如何打造一个好的(Vue)组件库?这里有一个清单

    2024-04-04 08:02:02       12 阅读
  2. 大模型中Prompt 攻击和防范

    2024-04-04 08:02:02       15 阅读
  3. 【Python整理】 Python知识点复习

    2024-04-04 08:02:02       13 阅读
  4. wordvect嵌入和bert嵌入的区别

    2024-04-04 08:02:02       13 阅读
  5. 运动伤害预防的实际案例

    2024-04-04 08:02:02       14 阅读
  6. 一次Postgres的实体表重构经历

    2024-04-04 08:02:02       14 阅读
  7. 走近Shiro--一起学习吧之架构

    2024-04-04 08:02:02       13 阅读
  8. 速盾:服务器有cdn 带宽上限建议多少

    2024-04-04 08:02:02       16 阅读