POJ 3414 Pots (BFS record Shortest path)

Source: Internet
Author: User
Topic links

POJ3414 Topic

Two bottles 1th and 2nd were given the capacity of A and b respectively, and a target water C, the following operations on two bottles:
Fill (i): Fill the bottle I
Drop (i): Pour the bottle I into the light
Pour (i,j): Pour the water of bottle I into the J bottle until the bottle water is poured out or the J bottle is filled
Ask at least a few times to make any bottle water volume C, and record any of the shortest operation path (special Judge). Output impossible if it is not possible. Analysis

The essence of the subject is still to seek the shortest steps, with BFS solution.
The state is the capacity of two bottles at the same time, so it needs to be stored in a two-dimensional array.
The expansion of the state is shown in the following diagram:

The challenge is to output a path. The processing method is to add a pre component to the struct array that represents the answer, to record which state the state is operating on, and therefore to number each state. Code

#include <iostream> #include <cstring> #include <string> #include <queue> #define MAXV 102 using N
Amespace std;   struct Node {int va,vb,step,num;
The step represents the number of operations required to expand to that state, and Num represents the status number};
    struct {string op;
int pre;
}ans[1000];//This array is used to output the path, ans[i] record I state is an int a,b,c,cnt=0 that is changed by the Ans[i].pre state for ANS[I].OP operation;
BOOL VIS[MAXV][MAXV];
String p[1000];
    void Bfs () {queue<node> Q;
    memset (vis,false,sizeof (VIS));
    Vis[0][0]=true;
    Node s;
    s.va=s.vb=s.step=s.num=0;
    Q.push (s); while (!
        Q.empty ()) {Node temp=q.front ();
        Q.pop (); if (temp.va==c| |
            TEMP.VB==C)//Reach target State {cout<<temp.step<<endl;
            int t=temp.num;
            int x=0;
                while (t!=0) {p[++x]=ans[t].op;
            T=ans[t].pre;
            }//because the operation of this access is in reverse, it is first deposited into the P array, and then the reverse output for (int i=x;i>=1;i--) cout<<p[i]<<endl;
 Return       } Node Temp2;
            /*fill (1) */if (temp.va!=a) {temp2.va=a;
            Temp2.vb=temp.vb;
                if (!vis[temp2.va][temp2.vb]) {vis[temp2.va][temp2.vb]=true;
                temp2.step=temp.step+1;
                temp2.num=++cnt;
                Q.push (TEMP2);
                Ans[cnt].op= "FILL (1)";
            Ans[cnt].pre=temp.num;
            }}/*fill (2) */if (temp.vb!=b) {Temp2.va=temp.va;
            Temp2.vb=b;
                if (!vis[temp2.va][temp2.vb]) {vis[temp2.va][temp2.vb]=true;
                temp2.step=temp.step+1;
                temp2.num=++cnt;
                Q.push (TEMP2);
                Ans[cnt].op= "FILL (2)";
            Ans[cnt].pre=temp.num;
            }}/*drop (1) */if (temp.va!=0) {temp2.va=0;
            Temp2.vb=temp.vb; if (!vis[temp2.va][Temp2.vb]) {vis[temp2.va][temp2.vb]=true;
                temp2.step=temp.step+1;
                temp2.num=++cnt;
                Q.push (TEMP2);
                Ans[cnt].op= "DROP (1)";
            Ans[cnt].pre=temp.num;
            }}/*drop (2) */if (temp.vb!=0) {Temp2.va=temp.va;
            temp2.vb=0;
                if (!vis[temp2.va][temp2.vb]) {vis[temp2.va][temp2.vb]=true;
                temp2.step=temp.step+1;
                temp2.num=++cnt;
                Q.push (TEMP2);
                Ans[cnt].op= "DROP (2)";
            Ans[cnt].pre=temp.num; }}/*pour */if (temp.va!=0&&temp.vb!=b) {if (b-temp.vb>=temp
                . va) {temp2.va=0;
            Temp2.vb=temp.vb+temp.va;
                } else {temp2.va=temp.va-(B-temp.vb); TEmp2.vb=b;
                } if (!vis[temp2.va][temp2.vb]) {vis[temp2.va][temp2.vb]=true;
                temp2.step=temp.step+1;
                temp2.num=++cnt;
                Q.push (TEMP2);
                Ans[cnt].op= "pour";
            Ans[cnt].pre=temp.num; }}/*pour (2,1) */if (temp.vb!=0&&temp.va!=a) {if (A-temp.va>=tem
                P.vb) {temp2.vb=0;
            Temp2.va=temp.va+temp.vb;
                } else {temp2.vb=temp.vb-(A-temp.va);
            Temp2.va=a;
                } if (!vis[temp2.va][temp2.vb]) {vis[temp2.va][temp2.vb]=true;
                temp2.step=temp.step+1;
                temp2.num=++cnt;
                Q.push (TEMP2);
                Ans[cnt].op= "pour (2,1)";
            Ans[cnt].pre=temp.num; }}} COUT&LT;&Lt; "
    Impossible "<<endl;
Return
    } int main () {cin>>a>>b>>c;
Bfs ();
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.