POJ 3414 Pots 广度优先搜索+记忆化

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

简单题目,纯属练习。。。跟这个类似的------>>>点击打开链接

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)
#include<stdio.h>
#include<string.h>
int a,b,p;
struct node
{
    int a,b,step;
    int pre,flag;
} q[1000],t,f;
int s[101][101];
void shuchu(int qian)
{
    int a[101];
    int k=0;
    for(int i=qian; i!=-1; i=q[i].pre)
    {
        a[k++]=q[i].flag;
    }
    for(int i=k-1;i>=0;i--)
    {
        if(a[i]==1)
            printf("FILL(1)\n");
        else if(a[i]==2)
            printf("FILL(2)\n");
        else if(a[i]==3)
            printf("DROP(1)\n");
        else if(a[i]==4)
            printf("DROP(2)\n");
        else if(a[i]==5)
            printf("POUR(1,2)\n");
        else if(a[i]==6)
            printf("POUR(2,1)\n");
    }
}
int bfs()
{
    int i;
    int qian=1;
    int hou=0;
    q[0].a=0;
    q[0].b=0;
    q[0].flag=-1;
    q[0].pre=-1;
    q[0].step=0;
    s[0][0]=1;
    while(qian>hou)
    {
        t=q[hou++];
        if(t.a==p||t.b==p)
        {
            printf("%d\n",t.step);
            shuchu(hou-1);
            return 0;
        }
        f.a=a;
        f.b=t.b;
        if(s[f.a][f.b]==0)
        {
            f.flag=1;
            f.pre=hou-1;
            f.step=t.step+1;
            q[qian++]=f;
            s[f.a][f.b]=1;
        }
        f.a=t.a;
        f.b=b;
        if(s[f.a][f.b]==0)
        {
            f.flag=2;
            f.pre=hou-1;
            f.step=t.step+1;
            q[qian++]=f;
            s[f.a][f.b]=1;
        }
        f.a=0;
        f.b=t.b;
        if(s[f.a][f.b]==0)
        {
            f.flag=3;
            f.pre=hou-1;
            f.step=t.step+1;
            q[qian++]=f;
            s[f.a][f.b]=1;
        }
        f.a=t.a;
        f.b=0;
        if(s[f.a][f.b]==0)
        {
            f.flag=4;
            f.pre=hou-1;
            f.step=t.step+1;
            q[qian++]=f;
            s[f.a][f.b]=1;
        }
        f.b=t.b+t.a;
        f.a=t.a-(b-t.b);
        if(f.b>=b)
            f.b=b;
        if(f.a<0)
            f.a=0;
        if(s[f.a][f.b]==0)
        {
            f.flag=5;
            f.pre=hou-1;
            f.step=t.step+1;
            q[qian++]=f;
            s[f.a][f.b]=1;
        }
        f.a=t.a+t.b;
        f.b=t.b-(a-t.a);
        if(f.a>=a)
            f.a=a;
        if(f.b<0)
            f.b=0;
        if(s[f.a][f.b]==0)
        {
            f.flag=6;
            f.pre=hou-1;
            f.step=t.step+1;
            q[qian++]=f;
            s[f.a][f.b]=1;
        }
    }
    printf("impossible\n");
    return 0;
}
int main()
{
    while(scanf("%d %d %d",&a,&b,&p)!=EOF)
    {
        memset(s,0,sizeof(s));
        bfs();
    }
    return 0;
}
时间: 2024-10-09 22:58:00

POJ 3414 Pots 广度优先搜索+记忆化的相关文章

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

广搜+输出路径 POJ 3414 Pots

POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   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)

POJ 3414 Pots (经典bfs )

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

POJ 1351 Number of Locks (记忆化搜索 状态压缩)

Number of Locks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1161   Accepted: 571 Description In certain factory a kind of spring locks is manufactured. There are n slots (1 < n < 17, n is a natural number.) for each lock. The height

POJ 3249 Test for Job (记忆化搜索 好题)

Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9512   Accepted: 2178 Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job

poj 1661 Help Jimmy(记忆化搜索)

题目链接:http://poj.org/problem?id=1661 一道还可以的记忆化搜索题,主要是要想到如何设dp,记忆化搜索是避免递归过程中的重复求值,所以要得到dp必须知道如何递归 由于这是个可以左右移动的所以递归过程肯定设计左右所以dp的一维为从左边下或者从右边下,而且和层数有关所以另一维为层数 于是便可以得到dp[count][flag],flag=1表示count层从左边下要多久,flag=0表示count层从右边下要多久.然后就是dfs的递归 过程 #include <iost

POJ 2192 &amp;&amp; HDU 1501 Zipper (记忆化搜索)

Zipper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16803   Accepted: 5994 Description Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first tw

poj 1579(动态规划初探之记忆化搜索)

Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17843   Accepted: 9112 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b

POJ 1351-Number of Locks(记忆化搜索)

Number of Locks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1140   Accepted: 559 Description In certain factory a kind of spring locks is manufactured. There are n slots (1 < n < 17, n is a natural number.) for each lock. The height