【迭代加深dfs】【bfs】Z‘s Coffee CSU - 2061 【三个杯子倒咖啡】

【迭代加深dfs】【bfs】Z‘s Coffee CSU - 2061 【三个杯子倒咖啡】


迭代加深dfs不会写,挖个坑,日后补,这里用的bfs


这里写图片描述

Z is crazy about coffee. One day he bought three cups of coffee. The first cup has a capacity of A ml, the second has B ml and the third has C ml. At the beginning, only the first cup is full of coffee, that is, Z only has A ml coffee and the other cups is empty. Because the cup has no scale, one operation of pouring coffee only stop until either one cup is full or empty. Z can only choose two cups to do the pouring operation. For some reasons, Z wants to get exactly D ml of coffee, and also he is so lazy that want the number of operations as least as possible. So he ask you for help.

Input
The first line is the case number T.

Each case has one line with four integers A B C D as mentioned above.

1 ≤ A, B, C ≤ 1000

1 ≤ D ≤ max(A, B, C)

1 ≤ T ≤ 100

Output
If he can get the exactly milliliter of coffee as he want, then print the least number of operation in a line.

And print the initial capacity of the three cups and then print the result after each operation line by line.

The print order should be the first cup, the second cup and the third cup.

If there are more than one operation schema, any of them will be accepted.

If he cannot get the exactly milliliter of coffee as he want , print “-1” without quotation.

Sample Input
1
12 8 5 10

Sample Output
5
12 0 0
7 0 5
0 7 5
5 7 0
5 2 5
10 2 0

题意:
有三个杯子ABC,容积分别为A,B,C,现在A是满的,要求三个杯子之间相互倒咖啡,最终得到一杯里面体积为D,如果可以得到,那么输出步骤最少的倒咖啡步骤,否则输出-1。

思路:

  • 因为三个杯子里面咖啡的总体积是固定的,始终为Aml,那么只要知道某两个杯子里咖啡体积,这个状态就固定了,那么我们可以 A B
  • 进行bfs搜索,每一次的操作为用一个杯子cup1向另外一个杯子cup2里倒咖啡,而到多少又有两种情况:

    • cup1里面的咖啡可以全部到给cup2,而cup2不会溢出。
    • cup1里面的咖啡不能全部到给cup2,只能倒一部分,直到cup2满为止。

    a , b , c
    i j a , b , c

  • 找到解后,如何打印结果呢?因为找到的解是最终的状态,需要一步步倒退回去,并打印,因此可以在用结构体保存状态的时,记录下步数,Id, I d I d

AC代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <stack>

using namespace std;

const int maxn = 1005;
int vis[maxn][maxn];
int bz[3], d;

struct coffee
{
    int cap[3], id, step, pre;
    coffee(){};
    coffee(int a, int b, int c, int idd, int st, int pp):id(idd), step(st),  pre(pp)
    {
        cap[0] = a;
        cap[1] = b;
        cap[2] = c;
    }
};

void bfs()
{
    memset(vis, 0, sizeof(vis));
    queue<coffee> q;
    coffee cof[1000000];
    int cnt = 0;
    cof[++cnt] = coffee(bz[0], 0, 0, 1, 0, -1);
    q.push(cof[1]);
    vis[bz[0]][0] = 1;
    while(!q.empty())
    {
        coffee s = q.front();
        q.pop();
        if(s.cap[0] == d || s.cap[1] == d || s.cap[2] == d)
        {
            printf("%d\n", s.step);
            stack<coffee> prt;
            int last = s.id;
            while(last != -1)
            {
                prt.push(cof[last]);
                last = cof[last].pre;
            }
            while(!prt.empty())
            {
                coffee tmp = prt.top();
                prt.pop();
                printf("%d %d %d\n", tmp.cap[0], tmp.cap[1], tmp.cap[2]);
            }
            return ;
        }
        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                coffee t = s;
                if(i == j || t.cap[i] == 0)
                    continue;
                if(t.cap[i] > bz[j] - t.cap[j])
                {
                    t.cap[i] -= (bz[j] - t.cap[j]);
                    t.cap[j] = bz[j];
                }
                else
                {
                    t.cap[j] += t.cap[i];
                    t.cap[i] = 0;
                }
                if(!vis[t.cap[0]][t.cap[1]])
                {
                    vis[t.cap[0]][t.cap[1]] = 1;
                    t.id = ++cnt;
                    t.pre = s.id;
                    t.step++;
                    cof[cnt] = t;
                    q.push(t);
                }
            }
        }
    }
    printf("-1\n");
}


int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d%d", &bz[0], &bz[1], &bz[2], &d);
        bfs();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Floraqiu/article/details/81262050