Poj 1606 jugs (BFS)

Source: Internet
Author: User

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 ;}

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.