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;
}
目录
相关文章
|
7月前
|
人工智能
POJ 3104 Drying
POJ 3104 Drying
|
人工智能 机器学习/深度学习
|
人工智能
POJ 2531
初学dfs参考别人代码,如有雷同,见怪不怪。#include using namespace std; int aa[25][25]; int maxa=0; int step[25]={0},n; void dfs(int a,int b) { int t=b; step...
676 0
|
机器学习/深度学习
|
存储
poj 1990 MooFest
点击打开poj 1990 思路: 树状数组 分析: 1 题目给定n头牛的听力v[i]. 现在规定两头你i和j如果要进行交流的话那么消耗的能量就是dis(i,j)*max(v[i].
729 0
poj 2337 Catenyms
点击打开链接poj2377 思路: 并查集+排序+欧拉道路 分析: 1 题目要求的是,是否可以组成欧拉道路并且输出字典序最小的方案 2 和别的题目不一样的是这一题的输出是最小的字典序,所以这里面是一个难点,那么我们应该怎么做呢?其实我们只要对输入的n个单词进行从小到达排序即可 3 然后我们先去判断该有向图是否是单连通的 4 我们去判断是否最多只有两个点的入度不等与出度,其余所有点的出度等于入度 5 如果都满足的话,进行dfs。
835 0

热门文章

最新文章