計蒜客——無腦博士的試管們

傳送門:https://nanti.jisuanke.com/t/31
時間限制1000ms 內存限制65536K

無腦博士有三個容量分別是 A,B,C 升的試管,A,B,C 分別是三個從 1 到 20 的整數,最初,A 和 B 試管都是空的,而 C 試管是裝滿硫酸銅溶液的。有時,無腦博士把硫酸銅溶液從一個試管倒到另一個試管中,直到被灌試管裝滿或原試管空了。當然每一次灌注都是完全的。由於無腦博士天天這麼折騰,早已熟練,溶液在倒的過程中不會有丟失。

寫一個程序去幫助無腦博士找出當 A 試管是空的時候,C 試管中硫酸銅溶液所剩量的所有可能性。

輸入格式
輸入包括一行,爲空格分隔開的三個數,分別爲整數 A,B,C。

輸出格式
輸出包括一行,升序地列出當 A 試管是空的時候,C 試管溶液所剩量的所有可能性。

樣例輸入

2 5 10

樣例輸出

5 6 7 8 9 10

#include <iostream>
#include <cstdio>
#include <set>

unsigned int A, B, C;

std::set<unsigned int> fin;
const unsigned int num = 25;
bool flag[num][num][num] = {false};

void DFS(unsigned int a, unsigned int b, unsigned int c);
int main()
{
    scanf("%d %d %d", &A, &B, &C);
    DFS(0, 0, C);
    for (std::set<unsigned int>::iterator it = fin.begin(); it != --fin.end(); ++it)
        std::cout << *it << " ";
    std::cout << *(--fin.end());
    return 0;
}

bool can(unsigned int x, unsigned int y, unsigned int X, unsigned int Y, int * temp) {
    if (x == 0 || y == Y) return false;
    if (Y - y >= x) {
        y += x;
        x = 0;
    }
    else {
        x -= Y - y;
        y = Y;
    }
    temp[0] = x;
    temp[1] = y;
    return true;
}

void DFS(unsigned int a, unsigned int b, unsigned int c) {
    if (flag[a][b][c]) return;
    if (a == 0) fin.insert(c);
    flag[a][b][c] = true;

    int temp[2];
    // a -> b
    if (can(a, b, A, B, temp)) DFS(temp[0], temp[1], c);
    // a -> c
    if (can(a, c, A, C, temp)) DFS(temp[0], b, temp[1]);
    // b -> a
    if (can(b, a, B, A, temp)) DFS(temp[1], temp[0], c);
    // b -> c
    if (can(b, c, B, C, temp)) DFS(a, temp[0], temp[1]);
    // c -> a
    if (can(c, a, C, A, temp)) DFS(temp[1], b, temp[0]);
    // c -> b
    if (can(c, b, C, B, temp)) DFS(c, temp[1], temp[0]);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章