ZOJ-1003-Jugs

1 问题描述

In the movie “Die Hard 3”, Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

fill A
fill B
empty A
empty B
pour A B
pour B A
success

where “pour A B” means “pour the contents of jug A into jug B”, and “success” means that the goal has been accomplished.

You may assume that the input you are given does have a solution.

Input

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

Output

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line “success”. Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

Sample Input

3 5 4
5 7 3

Sample Output

fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success

2 解题代码

#include <stdio.h>
#include <stdlib.h>

#define EMPTY -1
#define FULL 1
#define MIDDLE 0

#define START 0
#define FILL_A 1
#define FILL_B 2
#define EMPTY_A 3
#define EMPTY_B 4
#define POUR_A_B 5
#define POUR_B_A 6

#define BranchNodeType char
#define QueueNodeType State *

short int global_ca;  //全局变量,the capacities of the jugs A
short int global_cb;  //全局变量,the capacities of the jugs B
short int global_n;   //全局变量,n is the goal

typedef struct Branch  //分支树
{
    struct Branch *parent;  //指向父结点的指针
    BranchNodeType data;     //数据
    char child_num;         //记录当前结点的孩子总数
} Branch, BranchNode;

typedef struct State //代表A,B两个桶的状态
{
    Branch *ans_bch;    //答案分支树,记录到达当前状态的所经过的操作
    short int a;       //A桶水量
    short int b;       //B桶水量
    char a_status;      //A桶状态,-1代表空, 1代表满,0代表不空不满
    char b_status;      //B桶状态,-1代表空, 1代表满,0代表不空不满
} State;

typedef struct QNode //队列结点
{
    QueueNodeType data;
    struct QNode *next;
} QNode;
typedef struct Queue //队列记录
{
    struct QNode *front;
    struct QNode *rear;
} Queue;

//初始化一颗分支树
Branch *createBranch(void)
{
    BranchNode *b = malloc(sizeof(BranchNode));
    b->data = START;
    b->child_num = 0;
    b->parent = NULL;
    return b;
}

//删除当前分支
void disposeBranch(Branch *b)
{
    BranchNode *bp;

    while(b != NULL && b->child_num==0) { //when it run, b is always not null, and b has not child
        bp = b->parent;
        if(bp != NULL) bp->child_num -= 1; //make the child_num of b's parent decrease
        free(b);

        b = bp;
    }
}

//添加子分支
Branch *addBranch(Branch *parent, BranchNodeType step) //parentp must be not null
{
    BranchNode *b = malloc(sizeof(BranchNode));
    b->child_num = 0;
    b->data = step;
    b->parent = parent;

    parent->child_num += 1;
    return b;
}

//更改当前分支的数据
void changeBranch(Branch *b, BranchNodeType step)
{
    b->data = step;
    return ;
}

//打印从“原始分支”到达“当前分支”的路径
void print_route_branch(Branch *b)
{
    if(b != NULL) {
        print_route_branch(b->parent);

        switch(b->data)
        {
        case FILL_A:
            puts("fill A");
            break;
        case FILL_B:
            puts("fill B");
            break;
        case EMPTY_A:
            puts("empty A");
            break;
        case EMPTY_B:
            puts("empty B");
            break;
        case POUR_A_B:
            puts("pour A B");
            break;
        case POUR_B_A:
            puts("pour B A");
            break;
        default:
            ;
        }
    }
}

//初始化两个桶的状态
State *createState(void)
{
    State *s = malloc(sizeof(State));
    if(s==NULL) exit(0);

    s->a = 0;
    s->b = 0;
    s->a_status = EMPTY;
    s->b_status = EMPTY;
    s->ans_bch = createBranch();
    return s;
}

//释放AB桶状态的空间
void disposeState(State *s)
{
    if(s != NULL) {
        disposeBranch(s->ans_bch);
        free(s);
    }
}

//填满A桶
State *fill_a(State *s1)
{
    State *s2 = malloc(sizeof(State));
    *s2 = *s1;

    s2->a = global_ca;
    s2->a_status = FULL;

    s2->ans_bch = addBranch(s1->ans_bch, FILL_A);
    return s2;
}

//填满B桶
State *fill_b(State *s1)
{
    State *s2 = malloc(sizeof(State));
    *s2 = *s1;

    s2->b = global_cb;
    s2->b_status = FULL;

    s2->ans_bch = addBranch(s1->ans_bch, FILL_B);
    return s2;
    return s2;
}

//倒空A桶
State *empty_a(State *s1)
{
    State *s2 = malloc(sizeof(State));
    *s2 = *s1;

    s2->a = 0;
    s2->a_status = EMPTY;

    s2->ans_bch = addBranch(s1->ans_bch, EMPTY_A);
    return s2;
}

//倒空A桶
State *empty_b(State *s1)
{
    State *s2 = malloc(sizeof(State));
    *s2 = *s1;

    s2->b = 0;
    s2->b_status = EMPTY;

    s2->ans_bch = addBranch(s1->ans_bch, EMPTY_B);
    return s2;
}

//把水从A桶倒向B桶
State *pour_a_b(State *s1)
{
    State *s2 = malloc(sizeof(State));
    *s2 = *s1;

    int br = global_cb - s2->b; //剩余容量
    int a = s2->a;
    int change;
    if(br < a) {
        change = br;
        s2->a_status = MIDDLE;
        s2->b_status = FULL;
    }
    else if(br > a) {
        change = a;
        s2->a_status = EMPTY;
        s2->b_status = MIDDLE;
    }
    else {
        s2->a_status = EMPTY;
        s2->b_status = FULL;
        change = br;
    }
    s2->a -= change;
    s2->b += change;

    s2->ans_bch = addBranch(s1->ans_bch, POUR_A_B);
    return s2;
}

//把水从B桶倒向A桶
State *pour_b_a(State *s1)
{
    State *s2 = malloc(sizeof(State));
    *s2 = *s1;

    int ar = global_ca - s2->a; //剩余容量
    int b = s2->b;
    int change;
    if(ar < b) {
        change = ar;
        s2->a_status = FULL;
        s2->b_status = MIDDLE;
    }
    else if(ar > b) {
        change = b;
        s2->a_status = MIDDLE;
        s2->b_status = EMPTY;
    }
    else { //ar == br
        change = ar;
        s2->a_status = EMPTY;
        s2->b_status = FULL;
    }
    s2->b -= change;
    s2->a += change;

    s2->ans_bch = addBranch(s1->ans_bch, POUR_B_A);
    return s2;
}

//判断当前状态是否是目标状态
int isSuccessState(State *s1)
{
    if(s1->b == global_n) return 1;
    else return 0;
}

//打印到达当前状态的所经过的一系列操作
void print_route_state(State *s)
{
    print_route_branch(s->ans_bch);
    puts("success");
}

//初始化队列
Queue *createQueue()
{
    Queue *q = malloc(sizeof(Queue));
    if(q==NULL) exit(1);

    q->front = malloc(sizeof(QNode));
    if(q->front==NULL) exit(1);

    q->front->data = NULL;

    q->rear = q->front;
    q->front->next = NULL;

    return q;
}

//删除队列,释放队列所占用空间
void disposeQueue(Queue *q)
{
    if(q == NULL) exit(1);

    QNode *p1, *p2;
    p1 = q->front;

    while( p1 != NULL ) {
        p2 = p1->next;
        disposeState(p1->data);
        free(p1);
        p1 = p2;
    }

    free(q);
}

//入队
void enQueue(Queue *q, QueueNodeType x)
{
    QNode *newNode = malloc(sizeof(QNode));
    if(newNode==NULL) exit(1);

    newNode->data = x;
    newNode->next = NULL;

    q->rear->next = newNode;
    q->rear = newNode;
}

//出队
QueueNodeType deQueue(Queue *q)
{
    QNode *p = q->front->next; //p指向首元结点
    if(p == NULL)
        exit(0);
    else {
        if(q->rear == p) q->rear=q->front; //如果出栈的是最后一个结点,就修改尾指针使其指向头结点
        q->front->next = p->next; //头结点指向首元结点的下一个结点

        QueueNodeType ret = p->data;
        free(p);
        return ret;
    }

}

//判断队列是否为空
int isEmptyQueue(Queue *q)
{
    int ret = (q->front == q->rear);
    return ret;
}

int main(void)
{
    while(scanf("%hd %hd %hd", &global_ca, &global_cb, &global_n) != EOF)
    {
        Queue *q;
        State *s0, *s1;

        q = createQueue();
        s0 = createState();

        enQueue(q, s0);
        while(!isEmptyQueue(q))  //广度优先搜索
        {
            s0 = deQueue(q);      //记得释放s0
            if(isSuccessState(s0)) break;

            char lastStep = s0->ans_bch->data;

            //分支1,fill_a
            if(s0->a_status != FULL && lastStep != EMPTY_A) {
                s1 = fill_a(s0);
                enQueue(q, s1);
            }

            //分支2,fill_b
            if(s0->b_status != FULL && lastStep != EMPTY_B) {
                s1 = fill_b(s0);
                enQueue(q, s1);
            }

            //分支3,empty_a
            if(s0->a_status != EMPTY && lastStep != FILL_A) {
                s1 = empty_a(s0);
                enQueue(q, s1);
            }

            //分支4,empty_b
            if(s0->b_status != EMPTY && lastStep != FILL_B) {
                s1 = empty_b(s0);
                enQueue(q, s1);
            }

            //分支5, pour_a_b
            if(s0->a_status != EMPTY && s0->b_status != FULL && lastStep != POUR_B_A) {
                s1 = pour_a_b(s0);
                enQueue(q, s1);
            }

            //分支6,pour_b_a
            if(s0->b_status != EMPTY && s0->a_status != FULL && lastStep != POUR_A_B) {
                s1 = pour_b_a(s0);
                enQueue(q, s1);
            }

            disposeState(s0);  //释放s0
            s0 = NULL;
            s1 = NULL;
        } //while 广度优先搜索

        if(s0!=NULL) print_route_state(s0);

        disposeState(s0);
        disposeQueue(q);
        s0 = NULL;
        q = NULL;
    } //while

    return 0;
} //main */

3 Accepted截图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PLANET-01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值