poj3414

Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13545   Accepted: 5717   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.

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).

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’.

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

Poj3414

题目大意: 有二个水壶,对水壶有三种操作,1)FILL(i),将i水壶的水填满,2)DROP(i),将水壶i中的水全部倒掉,3)POUR(i,j)将水壶i中的水倒到水壶j中,若水壶 j 满了,则 i 剩下的就不倒了,问进行多少步操作,并且怎么操作,输出操作的步骤,两个水壶中的水可以达到C这个水量。如果不可能则输出impossible。初始时两个水壶是空的,没有水。

#include<stdio.h>
#include<string.h>

const int maxn = 110;
int vis[maxn][maxn]; //标记状态是否入队过
int a,b,c; //容器大小
int step; //最终的步数
int flag; //纪录是否能够成功

/* 状态纪录 */
struct Status{
    int k1,k2; //当前水的状态
    int op; //当前操作
    int step; //纪录步数
    int pre; //纪录前一步的下标
}q[maxn*maxn];
int id[maxn*maxn]; //纪录最终操作在队列中的编号
int lastIndex; //最后一个的编号

void bfs()
{
    Status now, next;

    int head, tail;
    head = tail = 0;

    q[tail].k1 = 0; q[tail].k2 = 0;
    q[tail].op = 0; q[tail].step = 0; q[tail].pre = 0;

    tail++;

    memset(vis,0,sizeof(vis));
    vis[0][0] = 1; //标记初始状态已入队

    while(head < tail) //当队列非空
    {
        now = q[head]; //取出队首
        head++; //弹出队首

        if(now.k1 == c || now.k2 == c) //应该不会存在这样的情况, c=0
        {
            flag = 1;
            step = now.step;
            lastIndex = head-1; //纪录最后一步的编号
        }

        for(int i = 1; i <= 6; i++) //分别遍历 6 种情况
        {
            if(i == 1) //fill(1)
            {
                next.k1 = a;
                next.k2 = now.k2;
            }
            else if(i == 2) //fill(2)
            {
                next.k1 = now.k1;
                next.k2 = b;
            }
            else if(i == 3) //drop(1)
            {
                next.k1 = 0;
                next.k2 = now.k2;
            }
            else if(i == 4) // drop(2);
            {
                next.k1 = now.k1;
                next.k2 = 0;
            }
            else if(i == 5) //pour(1,2)
            {
                if(now.k1+now.k2 <= b) //如果不能够装满 b
                {
                    next.k1 = 0;
                    next.k2 = now.k1+now.k2;
                }
                else //如果能够装满 b
                {
                    next.k1 = now.k1+now.k2-b;
                    next.k2 = b;
                }
            }
            else if(i == 6) // pour(2,1)
            {
                if(now.k1+now.k2 <= a) //如果不能够装满 a
                {
                    next.k1 = now.k1+now.k2;
                    next.k2 = 0;
                }
                else //如果能够装满 b
                {
                    next.k1 = a;
                    next.k2 = now.k1+now.k2-a;
                }
            }

            next.op = i; //纪录操作
            if(!vis[next.k1][next.k2]) //如果当前状态没有入队过
            {
                vis[next.k1][next.k2] = 1; //标记当前状态入队
                next.step = now.step+1; //步数 +1
                next.pre = head-1; //纪录前一步的编号

                //q.push(next);
                //q[tail] = next; 加入队尾
                q[tail].k1 = next.k1; q[tail].k2 = next.k2;
                q[tail].op = next.op; q[tail].step = next.step; q[tail].pre = next.pre;
                tail++; //队尾延长

                if(next.k1 == c || next.k2 == c) //如果达到目标状态
                {
                    flag = 1; //标记成功
                    step = next.step; //纪录总步骤数
                    lastIndex = tail-1; //纪录最后一步在模拟数组中的编号
                    return;
                }
            }
        }
    }

}

int main()
{
    while(scanf("%d%d%d", &a,&b,&c) != EOF)
    {
        flag = 0; //初始化不能成功
        step = 0;

        bfs();
        if(flag)
        {
            printf("%d\n", step);

            id[step] = lastIndex; //最后一步在模拟数组中的编号
            for(int i = step-1; i >= 1; i--)
            {
                id[i] = q[id[i+1]].pre; //向前找前一步骤在模拟数组中的编号
            }

            for(int i = 1; i <= step; i++)
            {
                if(q[id[i]].op == 1)
                    printf("FILL(1)\n");

                else if(q[id[i]].op == 2)
                    printf("FILL(2)\n");

                else if(q[id[i]].op == 3)
                    printf("DROP(1)\n");

                else if(q[id[i]].op == 4)
                    printf("DROP(2)\n");

                else if(q[id[i]].op == 5)
                    printf("POUR(1,2)\n");

                else if(q[id[i]].op == 6)
                    printf("POUR(2,1)\n");
            }
        }
        else printf("impossible\n");
    }
    return 0;
}
时间: 2024-10-16 23:29:45

poj3414的相关文章

poj3414 Pots(BFS+路径输出)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=3414 此题和poj1606一样 :http://blog.csdn.net/u012860063/article/details/37772275 Description You are given two pots, having the volume of A and B liters respectively.

Poj3414广搜

<span style="color:#330099;">/* D - D Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3414 Description You are given two pots, having the volume of A and B liters respectively. The follow

POJ3414(KB1-H BFS)

Pots Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour

Poj3414广泛搜索

<span style="color:#330099;">/* D - D Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3414 Description You are given two pots, having the volume of A and B liters respectively. The follow

简单搜索:poj3414

Pots 题意:两个容器,容量A,B,进行倒水操作,求最少的操作次数使得某一容器水量为C. 思路:一个多月前写的时候是没思路的,但是随着搜索算法的学习,慢慢懂得了一些 状态 的问题.这里,倒水就有很多的状态比如(FILL(A),B)(A, FILL(B))等.意识到这个之后,因为要求的是最少的操作次数,所以用bfs,注意剪枝.代码如下: 1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #incl

poj3414 Pots

Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour from

poj3414(bfs)

题目链接:http://poj.org/problem?id=3414 题意:给你两个容器 A  B 问是否能够经过有限的步骤倒水,得到容量为 C 的水,输出最小的步数,同时输出每一步的操作.如果不能达到目标状态,则输出  impossible. 分析:这题跟hdu1495一样,需要分情况考虑,不过这里回溯输出路径... 具体分析情况看这里:http://www.tuicool.com/articles/fqeI3i #include <cstdio> #include <cstring

POJ3414 Pots(BFS)

倒水问题. 题意:给两个杯子,容积分别为A和B.通过从水龙头接水(每次都接满),把杯里水全倒掉,把一个杯子里的水倒到另一个杯子里面三种操作使其中一个杯子里的水为C. 解题思路:6入口的BFS.难点在如何保存路径. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; bool vi

poj3414 Pots (BFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12198   Accepted: 5147   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f