Pots
Directly on Chinese
Descriptions:
Give you two containers, each liter of water can hold and A B premium, and may perform the following operations
FILL (i) the container is filled with the i-th (1 ≤ i ≤ 2) from the faucet;
The DROP (i) to the i-th vessel drained
POUR (i, j) the i-th container of water into the j th vessel (the two kinds of results after the end of the operation, a j-th one and the i-th container filling containers still have remaining, second the i-th all the water poured into the container j in the i-th container is empty)
Now ask you to write a program to find any of them can make a container of water happens to have C-liter, find the minimum number of operations and gives the operation
Input
There is only one row, comprising three numbers A, B, C (1 <= A, B <= 100, C <= max (A, B))
Output
The first line contains a number K represents a minimum number of operations
Then K lines each given a specific operation, if there are multiple answers to meet the minimum number of operations, the output of any operation of them, if you can not make any two containers meet exactly a C l, the output " impossible "
Sample Input
3 5 4
Sample Output
6 FILL (2) TO (2,1) DROP (1) TO (2.1) FILL (2) TO (2.1)
Topic links:
https://vjudge.net/problem/POJ-3414
bfs priority queue is OK, in 6 cases the discussion into the team, both on the specific solution to a problem the code, note the comments
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #include <sstream> #define mod 1000000007 #define eps 1e-6 #define ll long long #define INF 0x3f3f3f3f #define ME0 (X) Memset (X, 0, the sizeof (X)) the using namespace STD; int A, B, C; int In Flag = 0 ; // initialize unsuccessful int Total = 0 ; // total operation several times , it is the subscript of each step int ID [ 105 * 105 ]; // save the correct path index int VIS [ 105 ] [ 105 ]; // whether the flag state through the enqueue char OP [] [ 10 ] = { " 0 " , " FILL (. 1) " , " FILL (2)" , " The DROP (. 1) " , " the DROP (2) " , " POUR (1,2) " , " POUR (2,1) " }; // the last output struct Node { int NO; // this case standard int K1, K2; // cup remaining aqueous int OP; // operation int sTEP; // number of steps int pre; // previous step index BOOL operator <( const Node C &) const // small number of steps the first-out team { Return STEP> c.step; } }; The priority_queue <Node> Q; Node now, Next; Vector <Node> V; void Print ( int I) // output format { int J = 0 ; the while (V [I] .no) { ID [J ++] = V [I] .no; // store the correct path subscript I = V [I] .PRE; } for (J-I = . 1 ; I> = 0 ; I -) // index output in operation { COUT<<op[v[id[i]].op]<<endl; } } void bfs() { now.k1=0,now.k2=0,now.op=0,now.pre=0,now.step=0,now.no=0; vis[now.k1][now.k2]=1; q.push(now); v.push_back(now); while(!q.empty()) { now=q.top(); q.pop(); if(now.k1==c||now.k2==c)//满足条件 { flag=1; cout<<now.step<<endl;//输出步数 print(now.no);//输出格式 break; } for(int i=1; i<=6; i++) { if(i == 1) //fill(1) { next.k1 = a; next.k2 = now.k2; } else if(i == 2) //fill(2) { next.k1 = now.k1; next.k2 = b; } else if(i == 3) //drop(1) { next.k1 = 0; next.k2 = now.k2; } else if(i == 4) // drop(2); { next.k1 = now.k1; next.k2 = 0; } else if(i == . 5 ) // pour (1,2) { IF (+ now.k2 now.k1 <= B) // if we can not fill B { next.k1 = 0 ; next.k2 = now.k1 + now.k2; } the else // if you can fill B { next.k1 = now.k1 now.k2- + B; next.k2 = B; } } the else IF (I == . 6 ) // pour (2,1) { IF (Now.k1 + now.k2 <= A) // if we can not fill A { next.k1 = now.k1 + now.k2; next.k2 = 0 ; } the else // if you can fill B { Next .k1 = A; next.k2 = now.k1 now.k2- + A; } } next.op = I; IF (! VIS [next.k1] [next.k2]) { Total ++ ; next.no=total; vis[next.k1][next.k2]=1; next.step=now.step+1; next.pre=now.no; v.push_back(next); q.push(next); } } } if(flag==0) cout<<"impossible"<<endl; } int main() { ME0(vis); cin>>a>>b>>c; bfs(); }