Http://poj.org/problem? Id = 1606
If you have no idea, think about it. Remember that the current A and B volume and operations can be BFs, which is purely violent ..
The output path is required. You need to use a pointer to record the previous state of the current State in the node, and output the result according to the cursor.
Code:
# Include <cstdio>
# Include <cstring>
Int A, B, n, H, R;
Bool vis [1001] [1001];
Int ans [100001];
Struct node {
Int va, VB, numeric, pre;
} Queue [1000001];
Void output (){
Int I = 1;
Ans [0] = queue [H]. Queue;
H = queue [H]. Pre;
While (h ){
Ans [I ++] = queue [H]. Queue;
H = queue [H]. Pre;
}
For (Int J = I-1; j> = 0; j --){
Switch (ANS [J]) {
Case 1: printf ("fill a \ n"); break;
Case 2: printf ("fill B \ n"); break;
Case 3: printf ("empty a \ n"); break;
Case 4: printf ("Empty B \ n"); break;
Case 5: printf ("Pour a B \ n"); break;
Case 6: printf ("Pour B A \ n"); break;
}
}
Printf ("Success \ n ");
}
Void BFS (){
H = r = 0;
Queue [R]. Va = 0;
Queue [R ++]. VB = 0;
While (H! = R ){
Node T;
If (queue [H]. VB = N ){
Output ();
Return;
}
T. Va =;
T. VB = queue [H]. VB;
If (! Vis [T. Va] [T. VB]) {
Vis [T. Va] [T. VB] = true;
T. bytes = 1;
T. Pre = h;
Queue [R ++] = T;
}
T. Va = queue [H]. Va;
T. VB = B;
If (! Vis [T. Va] [T. VB]) {
Vis [T. Va] [T. VB] = true;
T. Counter = 2;
T. Pre = h;
Queue [R ++] = T;
}
T. Va = 0;
T. VB = queue [H]. VB;
If (! Vis [T. Va] [T. VB]) {
Vis [T. Va] [T. VB] = true;
T. Counter = 3;
T. Pre = h;
Queue [R ++] = T;
}
T. Va = queue [H]. Va;
T. VB = 0;
If (! Vis [T. Va] [T. VB]) {
Vis [T. Va] [T. VB] = true;
T. Counter = 4;
T. Pre = h;
Queue [R ++] = T;
}
If (queue [H]. Va-(B-queue [H]. VB) <0 ){
T. Va = 0;
T. VB = queue [H]. VB + queue [H]. Va;
}
Else {
T. Va = queue [H]. Va-(B-queue [H]. VB );
T. VB = B;
}
If (! Vis [T. Va] [T. VB]) {
Vis [T. Va] [T. VB] = true;
T. frequency = 5;
T. Pre = h;
Queue [R ++] = T;
}
If (queue [H]. VB-(A-queue [H]. Va) <0 ){
T. VB = 0;
T. Va = queue [H]. Va + queue [H]. VB;
}
Else {
T. VB = queue [H]. VB-(A-queue [H]. Va );
T. Va =;
}
If (! Vis [T. Va] [T. VB]) {
Vis [T. Va] [T. VB] = true;
T. bytes = 6;
T. Pre = h;
Queue [R ++] = T;
}
H ++;
}
}
Int main (){
While (~ Scanf ("% d", & A, & B, & N )){
Memset (VIS, false, sizeof (VIS ));
BFS ();
}
Return 0 ;}