• Pots bfs


    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 ≤ i ≤ 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 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)


    bfs,六种操作,代码中op数组给出,然后需要标记ab,同样的状态不重复记录。queue不太会用,还是用数组队列吧


    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <map>
    #include <cmath>
    #include <string>
    using namespace std;
    int a,b,c,ta,tb,flag,head=0,tail=0;
    string op[6]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
    struct que
    {
        int al,bl,f,c;
        int ope;
    }q[10000];
    int vis[101][101];///用这个有保障
    void print(int x)
    {
        if(q[x].f!=-1)print(q[x].f);
        else return;
        cout<<op[q[x].ope]<<endl;
    }
    int main()
    {
        ios::sync_with_stdio(false);
    cin.tie(0);
    q[tail].al=0,q[tail].bl=0,q[tail].f=-1,q[tail++].c=0; vis[0][0]=0; if(c) while(head<tail) { if(q[head].al==c||q[head].bl==c) { flag=1; cout<<q[head].c<<endl; print(head); break; } for(int i=0;i<6;i++) { flag=0; ta=q[head].al; tb=q[head].bl; if(i==0){ta=a;} else if(i==1){tb=b;} else if(i==2){ta=0;} else if(i==3){tb=0;} else if(i==4){(b-tb>=ta)?(tb+=ta,ta=0):(ta-=b-tb,tb=b);} else{a-ta>=tb?(ta+=tb,tb=0):(tb-=a-ta,ta=a);} if(vis[ta][tb])continue; vis[ta][tb]=1; q[tail].ope=i; q[tail].al=ta,q[tail].bl=tb,q[tail].f=head,q[tail++].c=q[head].c+1; } head++; } else {flag=1;cout<<0<<endl;} if(flag==0)cout<<"impossible"<<endl; }
  • 相关阅读:
    (续)在深度计算框架MindSpore中如何对不持续的计算进行处理——对数据集进行一定epoch数量的训练后,进行其他工作处理,再返回来接着进行一定epoch数量的训练——单步计算
    YAML文件简介
    训练集验证集测试集的概念
    泛化误差
    drawio的打开方法
    移动硬盘无法被电脑识别
    r5 3600相当于英特尔什么级别
    Ubuntu WPS字体缺失配置
    pytorch深度学习cpu占用太高
    常用的架构设计原则-云原生架构设计快速入门
  • 原文地址:https://www.cnblogs.com/8023spz/p/7246436.html
Copyright © 2020-2023  润新知