码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-02-28 23:04:34      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

题意:有二个水壶,对水壶有三种操作,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;
}


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

标签:

原文地址:http://blog.csdn.net/wweiainn/article/details/43991491

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!