POJ 3414 Pots, poj3414pots
Pots
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed med:
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 integerers 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.
Sample Input
3 5 4
Sample Output
6
FILL (2)
POUR (2, 1)
DROP (1)
POUR (2, 1)
FILL (2)
POUR (2, 1)
Source
Northeastern Europe 2002, Western Subregion
6 operations: 1: Fill 1 cup with water; 2: Fill 2 cups with water; 3: 1 cup with water; 4: 2; 1 cup with water; pour out a cup of water; pour out a cup of water idea: simple BFS + backtracking wide search borrow node subscript, Record Father's Day, record the corresponding number of operations, and end node back from the End Node, print path # include <cstdio> # include <queue> # include <cstring> # include <cstdlib> # include <vector> using namespace std; int a, B, c; int num, flag; // num records the number of Search Steps, flag records the number of end Search Steps int ans [101] [101]; // records the number of steps struct next {int op; // operate int par; // parent} nn [10001]; void dfs (int flag ){/ /Deep Search print path if (flag> 0) {dfs (nn [flag]. par);} if (nn [flag]. op = 1) cout <"FILL (1)" <endl; else if (nn [flag]. op = 2) cout <"FILL (2)" <endl; else if (nn [flag]. op = 3) cout <"POUR (1, 2)" <endl; else if (nn [flag]. op = 4) cout <"POUR (2, 1)" <endl; else if (nn [flag]. op = 5) cout <"DROP (1)" <endl; else if (nn [flag]. op = 6) cout <"DROP (2)" <endl;} bool bfs () {memset (ans, 0, siz Eof (ans); ans [0] [0] = 1; queue <int> A; queue <int> B; while (! A. empty () |! B. empty () {. pop (); B. pop ();}. push (0); B. push (0); int time = 0, tempa, tempb; // current number of nodes in time while (! A. empty () {int inita =. front (); int initb = B. front ();. pop (); B. pop (); // FILL (1) tempa = a; tempb = initb; if (! Ans [tempa] [tempb]) {// ensure that no access to. push (tempa); B. push (tempb); ans [tempa] [tempb] = ans [inita] [initb] + 1; nn [num]. op = 1; nn [num ++]. par = time; // Record Father's Day point if (tempa = c | tempb = c) {flag = num-1; // record End Node break ;}} // FILL (2) tempa = inita; tempb = B; if (! Ans [tempa] [tempb]) {. push (tempa); B. push (tempb); ans [tempa] [tempb] = ans [inita] [initb] + 1; nn [num]. op = 2; nn [num ++]. par = time; if (tempa = c | tempb = c) {flag = num-1; break ;}// POUR (1, 2) if (inita <= B-initb) tempa = 0; // The else tempa = inita-(B-initb); // The tempb = (initb + inita ); if (tempb> B) tempb = B; // be careful if (! Ans [tempa] [tempb]) {. push (tempa); B. push (tempb); ans [tempa] [tempb] = ans [inita] [initb] + 1; nn [num]. op = 3; nn [num ++]. par = time; if (tempa = c | tempb = c) {flag = num-1; break;} // POUR) if (initb <= a-inita) tempb = 0; else tempb = initb-(a-inita); tempa = (initb + inita); if (tempa>) tempa = a; if (! Ans [tempa] [tempb]) {. push (tempa); B. push (tempb); ans [tempa] [tempb] = ans [inita] [initb] + 1; nn [num]. op = 4; nn [num ++]. par = time; if (tempa = c | tempb = c) {flag = num-1; break;} // DROP (1) tempa = 0; tempb = initb; if (! Ans [tempa] [tempb]) {. push (tempa); B. push (tempb); ans [tempa] [tempb] = ans [inita] [initb] + 1; nn [num]. op = 5; nn [num ++]. par = time; if (tempa = c | tempb = c) {flag = num-1; break;} // DROP (2) tempb = 0; tempa = inita; if (! Ans [tempa] [tempb]) {. push (tempa); B. push (tempb); ans [tempa] [tempb] = ans [inita] [initb] + 1; nn [num]. op = 6; nn [num ++]. par = time; if (tempa = c | tempb = c) {flag = num-1; break ;}+ + time; // number of record nodes} if (flag) {printf ("% d \ n", max (ans [tempa] [tempb], ans [tempb] [tempa])-1 ); // ans [0] [0] = 1, so dfs (flag); return true;} return false;} int main () {while (~ Scanf ("% d", & a, & B, & c) {num = 1; flag = 0; if (! Bfs () cout <"impossible" <endl;} return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.