Nyoj 21 map's breadth-first reverse search problem

Source: Internet
Author: User

Question link: http://acm.nyist.net/JudgeOnline/problem.php? PID = 21

Ideas:

In the algorithm competition entry-level classic, this question has a train of thought analysis. This question is an implicit graph traversal. The state in each of the three cups can be considered as a node, through several pouring methods, the next layer of nodes is introduced. This example shows the solution.

The following problems need to be solved:

1. How to Solve pouring water

A: Through simulation, because there is no scale, each pouring of water can only fill up, and the size of pouring water depends on the maximum available volume of the container and existing water.

2. node Construction

A: defines the struct, generates a state, and constructs a node.

3. Wide search

A. Use the queue

Code One

# Include <iostream>
# Include <string>
# Include <cstring>
# Include <algorithm>
# Include <queue>
Using namespace STD;
Struct glass
{
Int State [3];
Int step;
Glass () {step = 0 ;}
};
Int cupmax [3], target [3];
Bool visited [105] [105] [105]; // to avoid time and space waste caused by the same status, pruning is required to mark the Array
Void pourwater (int K, int I, Glass & Cup) // pour water from I to K
{
Int yield = cupmax [k]-cup. State [k];
If (cup. State [I]> = yield)
{
Cup. State [k] + = yield;
Cup. State [I]-= yield;
}
Else
{
Cup. State [k] + = Cup. State [I];
Cup. State [I] = 0;
}
}
Bool istrue (glass T)
{
If (T. state [0] = target [0] & T. state [1] = target [1] & T. state [2] = target [2])
Return true;
Return false;
}
Int BFS ()
{
Int I, J, K;
Queue <glass> q;
Glass initial;
Initial. State [0] = cupmax [0];
Initial. State [1] = initial. State [2] = 0;
Q. Push (initial );
Memset (visited, false, sizeof (visited ));
Visited [initial. State [0] [0] [0] = true;
While (! Q. Empty ())
{
Glass node = Q. Front ();
Q. Pop ();
If (istrue (node ))
Return node. step;
For (I = 0; I <3; I ++)
{
For (j = 1; j <3; j ++)
{
K = (I + J) % 3;
If (node. State [I]! = 0 & node. State [k] <cupmax [k])
{
Glass newnode = node;
Pourwater (K, I, newnode );
Newnode. Step = node. Step + 1;
If (! Visited [newnode. State [0] [newnode. State [1] [newnode. State [2])
{
Visited [newnode. State [0] [newnode. State [1] [newnode. State [2] = true;
Q. Push (newnode );
}
}
}
}
}
Return-1;
}
Int main ()
{
Int test;
Cin> test;
While (test --)
{
Cin> cupmax [0]> cupmax [1]> cupmax [2];
Cin> target [0]> target [1]> target [2];
Cout <BFS () <Endl;
}
// System ("pause ");
Return 0;
}


Code Two

# Include <iostream>
# Include <cstring>
# Include <string>
# Include <algorithm>
# Include <queue>
Using namespace STD;
Int cupmax [3], target [3];
Struct glass
{
Int State [3];
Int step;
Glass () {step = 0 ;}
};
Bool visited [105] [105] [105];
Int min (int A, int B) {return A> B? B: ;}
Bool istrue (glass T)
{
If (T. state [0] = target [0] & T. state [1] = target [1] & T. state [2] = target [2])
Return true;
Return false;
}
Int BFS ()
{
Memset (visited, false, sizeof (visited ));
Glass initial;
Initial. State [0] = cupmax [0];
Initial. State [1] = initial. State [2] = 0;
Visited [initial. State [0] [0] [0] = true;
Queue <glass> q;
Q. Push (initial );
While (! Q. Empty ())
{
Glass node = Q. Front ();
Q. Pop ();
If (istrue (node ))
Return node. step;

/////////////////////// Simulate several water dumping methods respectively
// 0-1
If (node. State [0]> 0 & node. State [1] <cupmax [1])
{
Glass temp = node;
Int K = min (node. State [0], cupmax [1]-node. State [1]);
Temp. State [0]-= K;
Temp. State [1] + = K;
Temp. Step = node. Step + 1;
If (! Visited [temp. State [0] [temp. State [1] [temp. State [2])
{
Visited [temp. State [0] [temp. State [1] [temp. State [2] = true;
Q. Push (temp );
}
}
// 0-2
If (node. State [0]> 0 & node. State [2] <cupmax [2])
{
Glass temp = node;
Int K = min (node. State [0], cupmax [2]-node. State [2]);
Temp. State [0]-= K;
Temp. State [2] + = K;
Temp. Step = node. Step + 1;
If (! Visited [temp. State [0] [temp. State [1] [temp. State [2])
{
Visited [temp. State [0] [temp. State [1] [temp. State [2] = true;
Q. Push (temp );
}
}
// 1-0
If (node. State [1]> 0 & node. State [0] <cupmax [0])
{
Glass temp = node;
Int K = min (node. State [1], cupmax [0]-node. State [0]);
Temp. State [1]-= K;
Temp. State [0] + = K;
Temp. Step = node. Step + 1;
If (! Visited [temp. State [0] [temp. State [1] [temp. State [2])
{
Visited [temp. State [0] [temp. State [1] [temp. State [2] = true;
Q. Push (temp );
}
}
// 1-2
If (node. State [1]> 0 & node. State [2] <cupmax [2])
{
Glass temp = node;
Int K = min (node. State [1], cupmax [2]-node. State [2]);
Temp. State [1]-= K;
Temp. State [2] + = K;
Temp. Step = node. Step + 1;
If (! Visited [temp. State [0] [temp. State [1] [temp. State [2])
{
Visited [temp. State [0] [temp. State [1] [temp. State [2] = true;
Q. Push (temp );
}
}
// 2-0
If (node. State [2]> 0 & node. State [0] <cupmax [0])
{
Glass temp = node;
Int K = min (node. State [2], cupmax [0]-node. State [0]);
Temp. State [2]-= K;
Temp. State [0] + = K;
Temp. Step = node. Step + 1;
If (! Visited [temp. State [0] [temp. State [1] [temp. State [2])
{
Visited [temp. State [0] [temp. State [1] [temp. State [2] = true;
Q. Push (temp );
}
}
// 2-1
If (node. State [2]> 0 & node. State [1] <cupmax [1])
{
Glass temp = node;
Int K = min (node. State [2], cupmax [1]-node. State [1]);
Temp. State [2]-= K;
Temp. State [1] + = K;
Temp. Step = node. Step + 1;
If (! Visited [temp. State [0] [temp. State [1] [temp. State [2])
{
Visited [temp. State [0] [temp. State [1] [temp. State [2] = true;
Q. Push (temp );
}
}
}
Return-1;
}
Int main ()
{
Int test;
Cin> test;
While (test --)
{
Cin> cupmax [0]> cupmax [1]> cupmax [2];
Cin> target [0]> target [1]> target [2];
Cout <BFS () <Endl;
}
// System ("pause ");
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.