Simple POJ 3414 pots,bfs+ record path.

Source: Internet
Author: User
Tags faa

Description

You are given the pots of 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 was full (and there may be some water left in the pot I), or the PO T I was empty (and all its contents has been moved to the pot J).

Write a program to find the shortest possible sequence of these operations that'll yield exactly C liters of Water in one of the pots.

is to operate on two cups, fill, empty, pour over. The water in the two cups represents a state, then the BFS is good, and to record the path, remember each father, and then finally come back to it all over again.

The code is as follows:

#include <iostream>#include<cstring>#include<cmath>#include<queue>using namespacestd;inta,b,c;intrem[ the][ the];intshorem[10005];structstate{intb; inttype; intFaa,fab; intnum; State () {}};state sta[ the][ the];voidShowans (state *e) {    intcou=0;  while(e->a| | E->b) {Shorem[cou++]=e->type; E=&sta[e->faa][e->Fab]; } cout<<cou<<Endl;  for(inti=cou-1; i>=0;--i)Switch(Shorem[i]) { Case 1: cout<<"FILL (1) \ n";  Break;  Case 2: cout<<"FILL (2) \ n";  Break;  Case 3: cout<<"DROP (1) \ n";  Break;  Case 4: cout<<"DROP (2) \ n";  Break;  Case 5: cout<<"pour () \ n";  Break;  Case 6: cout<<"pour (2,1) \ n";  Break; }}voidSlove () {Queue<state *>que; State*temp; intT1,t2,t3; sta[0][0].faa=sta[0][0].fab=-1; sta[0][0].type=0; sta[0][0].num=0; Que.push (&sta[0][0]);  while(!Que.empty ()) {Temp=Que.front ();        Que.pop (); if(temp->a==c| | temp->b==C) {Showans (temp); return; } T1=temp->A; T2=temp->b; if(sta[a][t2].num==-1) {Sta[a][t2].num=temp->num+1; STA[A][T2].FAA=T1; Sta[a][t2].fab=T2; Sta[a][t2].type=1; Que.push (&Sta[a][t2]); }        if(sta[t1][b].num==-1) {Sta[t1][b].num=temp->num+1; STA[T1][B].FAA=T1; Sta[t1][b].fab=T2; Sta[t1][b].type=2; Que.push (&Sta[t1][b]); }        if(sta[0][t2].num==-1) {sta[0][t2].num=temp->num+1; sta[0][t2].faa=T1; sta[0][t2].fab=T2; sta[0][t2].type=3; Que.push (&sta[0][t2]); }        if(sta[t1][0].num==-1) {sta[t1][0].num=temp->num+1; sta[t1][0].faa=T1; sta[t1][0].fab=T2; sta[t1][0].type=4; Que.push (&sta[t1][0]); } T3=min (t1,b-T2); if(sta[t1-t3][t2+t3].num==-1) {sta[t1-t3][t2+t3].num=temp->num+1; Sta[t1-t3][t2+t3].faa=T1; Sta[t1-t3][t2+t3].fab=T2; Sta[t1-t3][t2+t3].type=5; Que.push (&sta[t1-t3][t2+T3]); } T3=min (t2,a-t1); if(sta[t1+t3][t2-t3].num==-1) {sta[t1+t3][t2-t3].num=temp->num+1; Sta[t1+t3][t2-t3].faa=T1; Sta[t1+t3][t2-t3].fab=T2; Sta[t1+t3][t2-t3].type=6; Que.push (&sta[t1+t3][t2-T3]); }} cout<<"impossible\n";}intMain () {Ios::sync_with_stdio (false);  for(intI=0; i<= -;++i) for(intj=0; j<= -;++j) {sta[i][j].a=i; STA[I][J].B=J; }     while(cin>>a>>b>>C) { for(intI=0; i<= -;++i) for(intj=0; j<= -;++j) Sta[i][j].num=-1;    Slove (); }    return 0;}
View Code

Simple POJ 3414 pots,bfs+ record path.

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.