Depth and breadth priority oil problem (C # Implementation)
Source: Internet
Author: User
Problem of oil-splitting problem
-, description of the problem
Oil problem: Two children go to Polish, one with a pound of empty bottles, the other with a 72 and an empty bottle of 32. Originally planned each pound of oil, but because the money is not enough, had to fight a pound of oil, on the way home, two people want to share this catty oil, but there is no other tools. Now only use these three bottles (one catty, 72, 32) to accurately separate two and a half kilograms of oil.
Second, the algorithm description
F algorithm Selection
By analyzing the problems and combining the features of depth first, breadth first and iterative deepening search algorithm, this paper chooses breadth-first algorithm to solve the problem. If the depth-first algorithm is used to search, it is not necessarily possible to obtain the solution even if the solution is not the optimal one, because its blindness leads to the local trap of the search. Iterative deepening search is a combination of depth and breadth search in fixed depth, which avoids the shortcoming of single depth search, but the solution is not necessarily the optimal solution. Breadth first takes the space cost and the time cost as the guarantee to obtain the optimal solution. Because the problem is not complex, even if the use of breadth-first algorithm does not occupy too much space and time, so in order to obtain the optimal solution here, select the breadth-first algorithm to solve.
F Algorithm Description
1. Use Unvisitedbttsarr to represent the initial node list (to be extended, this is a dynamic array)
2. If Unvisitedbttsarr is the empty set, exit and give a signal of failure
3. N takes the first node of the Unvisitedbttsarr and deletes the node n in the Unvisitedbttsarr, placing the list of visited nodes Havevisitedbttsarr
4. If n is the target node, exit and give a success signal
5. Otherwise, add the child nodes of N to the end of N and return 2 steps
F Problem Analysis
L Select the appropriate data structure to represent the problem state
F vector (t,s,r) indicates that the state--t represents the amount of oil in the 102 bottles, S indicates the amount of oil in the 72 bottles, and R indicates the amount of oil in the 32 bottles.
The starting state of the F problem: (10,0,0).
The target state of the F problem: (5,2,3) or (5,3,2) or (5,5,0).
L Determine the intelligent operator to represent the rules of the changing state. Since the total oil quantity is 102, and 102 of the bottle can be filled with all the oil, so you can take 102 of the bottle as a lard bucket, so this problem will be the same as the previous 8/6 similar problems. Therefore, when describing the state of change, only 7, 3 bottles of the state can be given, 10 bottles of the state is the 10-s-r oil volume. However, due to the consistency of the program processing, in the implementation of the program I still 10, 8, 6 of the bottle unified treatment, rather than two states.
Resolution
Rules
Explain
1
(S,r) and S<7à (7,R)
7 kg bottle dissatisfied fashion full
2
(s,r) and R <3à (s,3)
3 kg bottle dissatisfied fashion full
3
(s,r) and S >0à (0,r)
7 kg empty when the bottle emptied
4
(s,r) and R >0à (s,0)
3 kg empty when the bottle emptied
5
(S,r) and s>0 and S+r≤3à (0,s+r)
7 kilograms of oil in the bottle poured into 3 kg bottle
6
(s,r) and R >0 and S+r≤7à (s+r,0)
3 kilograms of oil in the bottle poured into 7 kg bottle
7
(S,r) and S<7 and S+r≥7à (7, s+r-7)
With 3 kg of bottle of oil filled 7 kg bottle
8
(s,r) and R <3 and S+r≥3à (s+r-3,3)
With 7 kg of bottle of oil filled 3 kg bottle
Third, program design
The algorithm is implemented using the C # language. The program is mainly based on the breadth-first algorithm described above to implement the algorithm. There are four classes in the program:
Bottle class, used to describe the state of a bottle and some behavioral actions and properties.
Widthsearch class is the implementation class of breadth-first search algorithm
Depthsearch class is the implementation class of depth-first search algorithm
MainForm class, is the interface design of the class.
Here are two algorithms that are implemented primarily to make comparisons.
The following is a brief introduction to the program implementation of several core algorithms.
Bottle type
public class Bottle
{
int Capability = 0;//Total capacity of the bottle
int current = 0;//The amount of oil in the bottle
int remain = 0;//The amount remaining in the bottle
Action to pour into oil
public void Add (int val)
{
Current + = Val;
Remain-= Val;
}
Action to pour out oil
public void Sub (int val)
{
Current-= Val;
remain + = Val;
}
Depth first algorithm implementation class
public class Depthsearch
public void searching ()
{
Random r = new Random ();
while (Bottle_10.currentval!= 5)//Determine if the target status is reached
{
int random = R.next (1,7);//randomly determine the next child state with randomly generated numbers
Switch (random)
{
Case 1:
Flowto (bottle_03,bottle_07);
Break
Case 2:
Flowto (BOTTLE_10,BOTTLE_03);
Break
Case 3:
Flowto (BOTTLE_07,BOTTLE_03);
Break
Case 4:
Flowto (bottle_10,bottle_07);
Break
Case 5:
Flowto (BOTTLE_03,BOTTLE_10);
Break
Case 6:
Flowto (BOTTLE_07,BOTTLE_10);
Break
Default:
Break
}
if (!statearr.contains (Bottlesstate ()))
{
Statearr.add (Bottlesstate ());
}
Else
{
Returntoprestate ();
}
}
}
The action of pouring oil. This is from Bottle1 down to Bottle2.
while (unvisitedbttsarr.count>=0)//No Access table if a node continues to cycle the search, otherwise jump out
{
TreeNode n = (TreeNode) unvisitedbttsarr[0];
BOOL flag = TRUE;
Check if you have visited
foreach (TreeNode i in Havevisitedbttsarr)
{
if (I.text.equals (N.text))
{
Havevisitedbttsarr.add (Unvisitedbttsarr[0]);
Unvisitedbttsarr.removeat (0);
Flag = false;
Break
}
}
If you have already traversed, you do not need to continue the traversal jump to the next
if (flag)
{
if (Search (n))
{
Return
}
}
}
}
Create the child nodes and add them to the Unvisitedbttsarr.
private bool CreateNode (TreeNode node)
{
TreeNode n = new TreeNode (Bottlesstate ());
Unvisitedbttsarr.add (n);
if (Bottle_10.currentval = 5)
{
Node. Nodes.Add (n);
SetPath (n);
return true;
}
Node. Nodes.Add (n);
return false;
}
Backtracking to get the best path
private void SetPath (TreeNode N)
{
while (N.parent!=null)
{
Path = N.text + "->" + path;
N.forecolor = System.Drawing.Color.Blue;
n = n.parent;
}
}
Iv. results of the test
The test results are as follows:
1 Depth-first random generation of sub-node search path
2 breadth-First algorithm implementation diagram
From the above two results, if the existence of the solution breadth first always find the best solution, but from time to see depth priority faster and breadth first need to traverse each node to cause time space is more expensive, so time is definitely spent more. But it can be guaranteed to find the optimal solution. Because it is simpler and less complex, it is only possible to find the optimal solution in the nineth step, so the depth first is desirable, but if it is in some complex problems, this algorithm may lead to the combination explosion, the space is too large to cause the algorithm is not feasible.
Please advise. To source the program can be emailed to me, the code is not optimized.
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