poj 3414 Pots

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9985 Accepted: 4192 Special Judge

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

给你两个锅,各自的容积为A公升和B公升。下面是可以执行的动作:

1.FILL(i)   向锅i (1 ≤ ≤ 2) 里面灌水

2.DROP(i) 把锅里的水倒进下水道

3.POUR(i,j) 把i里面的水倒到j里面;直到把j倒满,或者把i倒空。

写一个程序找到一个使用最少的操作,能将任意一个容器的水达到C升。

Input

On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

只有一行,输入A,B,C。范围都在1 to 100 and C≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

第一行输出,得操作多少次K,接下来K行来描述,如果有几种完成方式,输出任意一种。如果没办法完成,输出impossible’。

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

Source

Northeastern Europe 2002, Western Subregion
这是一道比较典型又有点费劲的bfs。。。主要是思路清晰,然后能把函数复用明白,程序就不会太复杂。。。我就是复用不明白导致程序太复杂的典型例子。。。结果出错了很难定位。。。后来借用了别人的超大测试数据发现了问题。。。竟然有个地方vis写的完全不对,但是过了样例,只能说状态太多,一个测试数据覆盖不全面很难发现问题了。。。
#include <stdio.h>
#include <string.h>


struct Pots{
    int volume[2];
    int step;
    int pre;
    int opt;//0->a_fill;1->b_fill;2->a_pour_b;3->b_pour_a;4->a_drop;5->b_drop;
};

int a, b, c;
Pots fifo[33333];

int min_num(int a, int b);
void Fill(Pots *tmp, int head, int *tail, int i, int vis[111][111]);
void Drop(Pots *tmp, int head, int *tail, int i, int vis[111][111]);
void Pour(Pots *tmp, int head, int *tail, int i, int vis[111][111]);
void output(Pots *tmp);

int main(void){

    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    //
    int vis[111][111];
    int head = 0, tail = 1;
    int i;
    scanf("%d%d%d", &a, &b, &c);

    memset(vis, 0, sizeof(vis));
    fifo[head].step = 0;
    fifo[head].pre = 0;
    fifo[head].volume[0] = 0;
    fifo[head].volume[1] = 0;
    vis[0][0] = 1;

    while(head < tail){
        Pots tmp = fifo[head];
        if (tmp.volume[0] == c || tmp.volume[1] == c){
            output(&tmp);

            return 0;
        }
        for (i = 0;i < 2;i ++) {
            //printf("head = %d, volume[0]=%d, volume[1]=%d\n", head, tmp.volume[0], tmp.volume[1]);
            Fill(&tmp, head, &tail, i, vis);
            Drop(&tmp, head, &tail, i, vis);
            Pour(&tmp, head, &tail, i, vis);
        }
        head ++;
    }

    printf("impossible\n");
}
void output(Pots *tmp){
    int cnt = 0;
    int loc[1111];
    int i;
    while(tmp->step){
        loc[cnt++] = tmp->opt;
        tmp = &fifo[tmp->pre];
    }

    printf("%d\n", cnt);
    for (i = cnt - 1;i >= 0;i --){
        switch (loc[i]){
            case 0: printf("FILL(1)\n");break;
            case 1: printf("FILL(2)\n");break;
            case 2: printf("POUR(1,2)\n");break;
            case 3: printf("POUR(2,1)\n");break;
            case 4: printf("DROP(1)\n");break;
            case 5: printf("DROP(2)\n");break;
        }
    }

}
void Fill(Pots *tmp, int head, int *tail, int i, int vis[111][111]){
    if (!i){
        if (tmp->volume[0] != a && (!vis[a][tmp->volume[1]])){
            fifo[*tail].volume[0] = a;
            fifo[*tail].volume[1] = tmp->volume[1];
            fifo[*tail].opt = 0;
            fifo[*tail].step = tmp->step + 1;
            fifo[*tail].pre = head;
            vis[a][fifo[*tail].volume[1]] = 1;
            //printf("fir : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
            (*tail) ++;
        }
    }else{
        if (tmp->volume[1] != b && (!vis[tmp->volume[0]][b])){
            fifo[*tail].volume[1] = b;
            fifo[*tail].volume[0] = tmp->volume[0];
            fifo[*tail].opt = 1;
            fifo[*tail].step = tmp->step + 1;
            fifo[*tail].pre = head;
            vis[fifo[*tail].volume[0]][b] = 1;
            //printf("sed : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
            (*tail) ++;
        }
    }
    return;
}

void Drop(Pots *tmp, int head, int *tail, int i, int vis[111][111]){
    int temp_min;
    if (!i){
        temp_min = min_num(tmp->volume[0], b - tmp->volume[1]);
        if (tmp->volume[0] != 0 && tmp->volume[1] != b && (!vis[tmp->volume[0] - temp_min][tmp->volume[1] + temp_min])){
            fifo[*tail].volume[0] = tmp->volume[0] - temp_min;
            fifo[*tail].volume[1] = tmp->volume[1] + temp_min;
            fifo[*tail].opt = 2;
            fifo[*tail].step = tmp->step + 1;
            fifo[*tail].pre = head;
            vis[fifo[*tail].volume[0]][fifo[*tail].volume[1]];
            //printf("thr : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
            (*tail) ++;
        }
    }else{
        temp_min = min_num(a - tmp->volume[0], tmp->volume[1]);
        if (tmp->volume[0] != a && tmp->volume[1] != 0 && (!vis[tmp->volume[0] + temp_min][tmp->volume[1] - temp_min])){
            fifo[*tail].volume[0] = tmp->volume[0] + temp_min;
            fifo[*tail].volume[1] = tmp->volume[1] - temp_min;
            fifo[*tail].opt = 3;
            fifo[*tail].step = tmp->step + 1;
            fifo[*tail].pre = head;
            vis[fifo[*tail].volume[0]][fifo[*tail].volume[1]];
            //printf("for : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
            (*tail) ++;
        }
    }
    return;
}

void Pour(Pots *tmp, int head, int *tail, int i, int vis[111][111]){
    if (!i)
        if (tmp->volume[0] != 0 && (!vis[0][tmp->volume[1]])){
            fifo[*tail].volume[0] = 0;
            fifo[*tail].volume[1] = tmp->volume[1];
            fifo[*tail].opt = 4;
            fifo[*tail].step = tmp->step + 1;
            fifo[*tail].pre = head;
            vis[fifo[*tail].volume[0]][fifo[*tail].volume[1]];
            //printf("fif : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
            (*tail) ++;
        }else if (tmp->volume[1] != 0 && (!vis[tmp->volume[1]][0])){
            fifo[*tail].volume[1] = 0;
            fifo[*tail].volume[0] = tmp->volume[0];
            fifo[*tail].opt = 5;
            fifo[*tail].step = tmp->step + 1;
            fifo[*tail].pre = head;
            vis[fifo[*tail].volume[0]][fifo[*tail].volume[1]];
            //printf("six : tail = %d, volume[0]=%d, volume[1]=%d\n", *tail, fifo[*tail].volume[0], fifo[*tail].volume[1]);
            (*tail) ++;
        }

}
int min_num(int a, int b){
    return a < b ? a : b;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值