当前位置:编程学习 > C/C++ >>

POJ 3414 Pots

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7438 Accepted: 3120 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) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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 A, B, 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
[Submit] [Go Back] [Status] [Discuss]
 
 
 
考察点:bfs 理清思路做就可以了
 
[cpp]  
#include <stdio.h>  
#include <string.h>  
struct queue  
{  
    int x,y;  
    int pre;  
}a[1000000];  
int status[150][150],sum[150][150],ope[150][150],A,B,C;  
int res[100000];  
int main()  
{  
    void bfs();  
    int i,j,n,m,s,t;  
    memset(status,0,sizeof(status));  
    memset(sum,0,sizeof(sum));  
    scanf("%d %d %d",&A,&B,&C);  
    bfs();  
    return 0;  
}  
void bfs()  
{  
    int base,top,i,j,x,y,xend,yend,key,n;  
    key=0;  
    base=top=0;  
    a[top].x=0; a[top++].y=0;  
    status[0][0]=1;  
    while(base<top)  
    {  
        x=a[base].x; y=a[base++].y;  
        if(x==C||y==C)  
        {  
            key=1;  
            break;  
        }  
        for(i=0;i<=5;i++)  
        {  
            if(i==0)  
            {  
                xend=A; yend=y;  
            }else if(i==1)  
            {  
                xend=0; yend=y;  
            }else if(i==2)  
            {  
                if(A-x==0)  
                {  
                    continue;  
                }  
                if((A-x)>=y)  
                {  
                    xend=x+y; yend=0;  
                }else  
                {  
                    xend=A; yend=y-(A-x);  
                }  
            }else if(i==3)  
            {  
                yend=B; xend=x;  
            }else if(i==4)  
            {  
                yend=0;  xend=x;  
            }else if(i==5)  
            {  
                if((B-y)==0)  
                {  
                    continue;  
                }  
                 if((B-y)>=x)  
                 {  
                     yend= y+x; xend=0;  
                 }else  
                 {  
                     yend=B; xend=x-(B-y);  
                 }  
            }  
            if(!status[xend][yend]&&xend>=0&&xend<=A&¥d>=0&¥d<=B)  
            {  
                status[xend][yend]=1;  
                sum[xend][yend]=sum[x][y]+1;  
                a[top].x=xend; a[top].y=yend;  
                a[top++].pre=base-1;  
                ope[xend][yend]=i;  
            }  
        }  
    }  
    if(!key)  
    {  
        printf("impossible\n");  
    }else  
    {  
        printf("%d\n",sum[x][y]);  
        base--; &nbs
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,