H-pots
Time limit:1000ms Memory limit:65536kb 64bit IO format:%i64d &%i64u
Submit Status Practice POJ 3414
Description
You are given the pots of the volume of A and B liters respectively. The following operations can be performed:
Fill (i) Fill the pot I (1≤i≤2) from the tap;
DROP (i) empty the pot I to the drain;
Pour (i,j) pour from pot I to pot J; After this operation either the pot J was full (and there could be some water left in the pot I), or the pot 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 on E of the pots.
Input
On the first and only line is the numbers A, B, and C. These is all integers in the range from 1 to 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 is several sequences of minimal length, output any one of them. If the desired result can ' t is 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)
Rujia's Little white book, just mentioned a concept, called implicit graph Search, the personal feel, is the search, not the common matrix or adjacency table storage diagram, but rather abstract diagram, take the case as an example, the state can be understood as the node of the graph, From this state to another State is to indicate that there is an edge between the two states, then you can use the search ideas to write, the state of the subject is two cups of water, such as the initial no water, that is (0,0), but there are three kinds of operations, so by (0,0) can be (a,0), (0,b) ... such as
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <
cmath> #include <queue> #include <vector> #include <map> #include <stack> using namespace std;
const int maxn=50005;
const int inf=200000;
#define Lson rt<<1,l,m #define Rson rt<<1|1,m+1,r template<class t>inline T Read (t&x) {char C;
while ((C=getchar ()) <=32);
BOOL Ok=false;
if (c== '-') Ok=true,c=getchar ();
for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';
if (OK) x=-x;
return x;
} template<class t> inline void Read_ (t&x,t&y) {read (x);
Read (y);
} template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;
if (x<10) putchar (x+ ' 0 ');
else write (X/10), Putchar (x%10+ ' 0 ');
} template<class t>inline void Writeln (T x) {write (x);
Putchar (' \ n ');
}//-------IO template------int a,b,c;
struct Node {int A, B;
int id; node (int x,int y,int d) {id=d;a=x;b=y;}};
BOOL vis[105][105];
int num=0;
int D[MAXN];
int P[MAXN];
BOOL OK;
Map<int,string> MP;
Map<int,string>::iterator it;
void BFs (node s) {queue<node> q;
Q.push (s);
memset (vis,false,sizeof (VIS));
memset (P,-1,sizeof (p));
Mp.clear ();
d[0]=0;
P[0]=-1;
Vis[s.a][s.b]=true;
Num=1;
while (!q.empty ()) {node S=q.front (); Q.pop (); if (s.a==c| |
S.b==c) {printf ("%d\n", D[s.id]);
It=mp.begin ();
Stack<string> SS;
for (int i=s.id;i!=-1;i=p[i]) {//printf ("(%d) \ n", i);
It=mp.begin ();
while (It->first!=i&&it!=mp.end ()) {it++;
} ss.push (It->second);
} ss.pop ();
while (!ss.empty ()) {printf ("%s\n", Ss.top (). C_STR ());
Ss.pop (); } ok=1;
Return
}//BFS six-side if (s.a<a&&!vis[a][s.b]) {vis[a][s.b]=true, which can be reached by hierarchical search;
Q.push (Node (a,s.b,num));
d[num]=d[s.id]+1;
Mp[num]= "FILL (1)";
P[num++]=s.id;
} if (S.b<b&&!vis[s.a][b]) {vis[s.a][b]=true;
Q.push (Node (s.a,b,num));
d[num]=d[s.id]+1;
Mp[num]= "FILL (2)";
P[num++]=s.id;
} if (s.a>0&&!vis[0][s.b]) {vis[0][s.b]=true;
Q.push (Node (0,s.b,num));
d[num]=d[s.id]+1;
Mp[num]= "DROP (1)";
P[num++]=s.id;
} if (S.b>0&&!vis[s.a][0]) {vis[s.a][0]=true;
Q.push (Node (s.a,0,num));
d[num]=d[s.id]+1;
Mp[num]= "DROP (2)";
P[num++]=s.id;
} if (s.a>0&&s.b<b) { int t=min (s.a,b-s.b);
if (!vis[s.a-t][s.b+t]) {vis[s.a-t][s.b+t]=true;
Q.push (Node (s.a-t,s.b+t,num));
d[num]=d[s.id]+1;
Mp[num]= "pour";
P[num++]=s.id;
}} if (s.a<a&&s.b>0) {int t=min (S.B,A-S.A);
if (!vis[s.a+t][s.b-t]) {vis[s.a+t][s.b-t]=true;
Q.push (Node (s.a+t,s.b-t,num));
d[num]=d[s.id]+1;
Mp[num]= "pour (2,1)";
P[num++]=s.id;
}}}} int main () {int n,m,i,j,k,t;
Freopen ("In.txt", "R", stdin);
Read (A);
Read (B);
Read (C);
ok=0;
BFS (Node (0,0,0));
if (!ok) printf ("impossible\n");
return 0;
}