• 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:

    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)

     

    题意:给你两个体积为a,b的空水瓶 问你能不能通过以上6种操作 装到c体积的水

    思路:倒水问题+在结构体里开一个数组记录路径

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<stack>
    #include<bitset>
    #include<cstdlib>
    #include<cmath>
    #include<set>
    #include<list>
    #include<deque>
    #include<map>
    #include<queue>
    #define ll long long int
    using namespace std;
    inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
    int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
    const int inf=0x3f3f3f3f;
    const ll mod=1e9+7;
    int v[2],c;
    bool vis[107][107];
    string way[6]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
    struct node{
        int a[2];
        int cnt;
        int path[200];
    };
    node t;
    void pour(int from,int to){
        int temp=t.a[from]+t.a[to];
        if(temp>=v[to]) t.a[to]=v[to];
        else t.a[to]=temp;
        t.a[from]=temp-t.a[to];
    }
    void fill(int i){
        t.a[i]=v[i];
    }
    void drop(int i){
        t.a[i]=0;
    }
    void bfs(){
        queue<node> q;
        t.a[0]=t.a[1]=t.cnt=0;
        q.push(t);
        while(!q.empty()){
            node temp=q.front();
            q.pop();
            if(temp.a[0]==c||temp.a[1]==c){
                cout<<temp.cnt<<endl;
                for(int i=1;i<=temp.cnt;i++)
                cout<<way[temp.path[i]]<<endl;
                return ;
            }
            for(int i=0;i<6;i++){
                if(i==0){
                    t=temp;
                    fill(0);
                    t.path[t.cnt+1]=0;
                }else if(i==1){
                    t=temp;
                    fill(1);
                    t.path[t.cnt+1]=1;
                }else if(i==2){
                    t=temp;
                    drop(0);
                    t.path[t.cnt+1]=2;
                }else if(i==3){
                    t=temp;
                    drop(1);
                    t.path[t.cnt+1]=3;
                }else if(i==4){
                    t=temp;
                    pour(0,1);
                    t.path[t.cnt+1]=4;
                }else if(i==5){
                    t=temp;
                    pour(1,0);
                    t.path[t.cnt+1]=5;
                }
                if(vis[t.a[0]][t.a[1]]) continue;
                vis[t.a[0]][t.a[1]]=1;
                t.cnt++;
                q.push(t);
            }
        }
        cout<<"impossible"<<endl;
    }
    int main(){
        ios::sync_with_stdio(false);
        while(cin>>v[0]>>v[1]>>c){
            memset(vis,0,sizeof(vis));
            bfs();
        }
        return 0;
    }

     

  • 相关阅读:
    PID控制原理和算法
    Android 按钮长按下去重复执行某个动作,放开后停止执行动作
    最小二乘法拟合非线性函数及其Matlab/Excel 实现
    Android 基于蓝牙的方向控制器
    Android/Java 中的 String, StringBuffer, StringBuilder的区别和使用
    input 只允许输入数字
    java scoket编程
    RBAC权限管理
    整理大型网站架构必知必会的几个服务器知识
    Java中Class<T>与Class<?>的区别
  • 原文地址:https://www.cnblogs.com/wmj6/p/10461055.html
Copyright © 2020-2023  润新知