F-POJ-3414 Pots

简介: POJ-3414 Time Limit:1000 ms Memory Limit:65536 KDescription You are given two po...

POJ-3414

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)

BFS,然后模拟一个树来存走过的路径,压入栈。

//AC: 16MS  740KB
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
char S[6][10]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct Node{
    int step;
    int a,b;
};
struct {
    int num;
    int parent;
}path[1000000];         //模拟树存路径
bool vis[105][105];
void BFS(int A,int B,int C){
    Node now,next;
    memset(vis,false,sizeof(vis));
    now.step=now.a=now.b=0;
    int j=0,k=-1;
    vis[0][0]=true;
    queue<Node>Q;
    stack<int>N;
    Q.push(now);
    while(!Q.empty()){
        now=Q.front();
        k++;
        Q.pop();
        if(now.a==C||now.b==C){
            N.push(path[k].num);
            while(path[k].parent){
                k=path[k].parent;
                N.push(path[k].num);
            }
            printf("%d\n",now.step);
            for(int i=1;i<=now.step;i++){
                k=N.top();
                N.pop();
                printf("%s\n",S[k]);
            }
            return;
        }
        for(int i=0;i<6;i++){
            if(i==0){
                next.a=A;
                next.b=now.b;
                if(!vis[next.a][next.b]){
                    next.step=now.step+1;
                    Q.push(next);
                    vis[next.a][next.b]=true;
                    j++;
                    path[j].num=i;
                    path[j].parent=k;
                }
            }
            if(i==1){
                next.a=now.a;
                next.b=B;
                if(!vis[next.a][next.b]){
                    next.step=now.step+1;
                    Q.push(next);
                    vis[next.a][next.b]=true;
                    j++;
                    path[j].num=i;
                    path[j].parent=k;
                }
            }
            if(i==2){
                next.a=0;
                next.b=now.b;
                if(!vis[next.a][next.b]){
                    next.step=now.step+1;
                    Q.push(next);
                    vis[next.a][next.b]=true;
                    j++;
                    path[j].num=i;
                    path[j].parent=k;
                }
            }
            if(i==3){
                next.a=now.a;
                next.b=0;
                if(!vis[next.a][next.b]){
                    next.step=now.step+1;
                    Q.push(next);
                    vis[next.a][next.b]=true;
                    j++;
                    path[j].num=i;
                    path[j].parent=k;
                }
            }
            if(i==4){
                next.a=now.a-(B-now.b);
                if(next.a<0)
                    next.a=0;
                next.b=now.b+now.a;
                if(next.b>B)
                    next.b=B;
                if(!vis[next.a][next.b]){
                    next.step=now.step+1;
                    Q.push(next);
                    vis[next.a][next.b]=true;
                    j++;
                    path[j].num=i;
                    path[j].parent=k;
                }
            }
            if(i==5){
                next.b=now.b-(A-now.a);
                if(next.b<0)
                    next.b=0;
                next.a=now.a+now.b;
                if(next.a>A)
                    next.a=A;
                if(!vis[next.a][next.b]){
                    next.step=now.step+1;
                    Q.push(next);
                    vis[next.a][next.b]=true;
                    j++;
                    path[j].num=i;
                    path[j].parent=k;
                }
            }
        }
    }
    printf("impossible\n");
}
int main(){
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    BFS(a,b,c);
    return 0;
}
目录
相关文章
|
存储
|
C语言
poj 2503 查字典
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language.
866 0
poj 3620
题意:给出一个矩阵,其中有些格子干燥、有些潮湿。       如果一个潮湿的格子的相邻的四个方向有格子也是潮湿的,那么它们就可以构成更大       的湖泊,求最大的湖泊。       也就是求出最大的连在一块儿的潮湿的格子的数目。
573 0
poj-2551-ones
Description Given any integer 0
773 0
poj-1006-Biorhythms
Description 人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。
621 0
POJ 2027 No Brainer
Problem Description Zombies love to eat brains. Yum. Input The first line contains a single integer n indicating the number of data sets.
868 0
poj题目分类
http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html
770 0
|
算法 数据建模 机器学习/深度学习
poj1273Drainage Ditches
1 #include 2 /* 3 题意:就是寻找从源点到汇点的最大流! 4 要注意的是每两个点的流量可能有多个,也就是说有重边,所以要把两个点的所有的流量都加起来 5 就是这两个点之间的流量了! 6 ...
851 0