In depth priority search, the larger the depth, the better the node, the more it is expanded. If you change it to the depth, the better the node, the better, it is the breadth priority search method.
Basic ideas of breadth priority search algorithms:
(1) Establish an empty status queue SS;
(2) Establish an empty status library SB;
(3) Put the initial state S (0) into the queue SS;
(4) If the queue state is the target state, the search is successful, the algorithm is stopped. If this is the form of S (PATH), the solution is (PATH);
(5) If some kind of search limits have reached (such as space delivery, etc.), the search failed, the algorithm ends, no solution;
(6) Take a rule RN that can be applied to the first state S (PATH) and generate a suitable new state, generate a new state T (PATH, N), and places it in SS, Turn (4). If the expansion failed, that is, there is no new state generation, the first state in SS is removed from the SS, and the SB is sent to SB, and the step is executed;
(7) If the SS is an air queue, the search failed, the algorithm is running, and there is no solution. Otherwise turn (5).
Note: In the actual solution, the algorithm may have many different variations, and it is necessary to depend on the specific situation. Give an algorithm for reference:
The breadth priority basic algorithm is as follows:
Program bfs;
Initialization; establish a database data; initial state deposit into the database;
Set queue first pointer closed: = 0; queue tail pointer open: = 1;
Repeat
Remove the node of the next closed;
For r: = 1 to rmax do {r is a rule number}
Begin
IF sub-point meetings THEN
Begin
Open incremented 1, deposit the new node into the data library team;
IF new node and original nodes repeat thein
Delete this node (Open minus 1)
Else
The new node of IF is the target THEN output and exits;
end
end
Until Closed> = Open {queue empty};
Significant features of breadth priority search are that when new sub-nodes are generated, the smaller the depth is getting prioritized, that is, the sub-node is produced first. When nodes go to the cost of root nodes and the depth of the node, especially when each node to the root node is equal to the depth value, it is the best solution to the proposed solution. However, as long as the above algorithm is improved, the optimal solution can be obtained, which has become a price priority search.
Below is a 24-point program code:
#include
#include
#include
Using namespace std;
Const Double precision = 1e-6;
Const int count_of_number = 4;
Const int Number_to_be_cal = 24;
Double Number [count_of_number];
String expness [count_of_number];
Bool Search (int N)
{
IF (n == 1) {
IF (Fabs (Number [0] - Number_to_be_cal) Return True; } Else { Return False; } } For (int i = 0; i For (int J = i 1; j Double A, B; String EXPA, EXPB; A = Number [i]; B = Number [J]; Number [J] = Number [n - 1]; EXPA = Expression [i]; ExpB = expression [j]; Expression [J] = Expression [N - 1]; Expression [i] = '(' EXPA ' ' EXPB ')'; Number [i] = a b; IF (SEARCH (N - 1)) Return True; Expression [i] = '(' EXPA '-' EXPB ')'; Number [i] = a - b; IF (SEARCH (N - 1)) Return True; Expression [i] = '(' EXPB '-' EXPA ')'; Number [i] = b - a; IF (SEARCH (N - 1)) Return True; Expression [i] = '(' EXPA '*' EXPB ')'; Number [i] = a * b; IF (SEARCH (N - 1)) Return True; IF (b! = 0) { Expression [i] = '(' EXPA '/' EXPB ')'; Number [i] = a / b; IF (SEARCH (N - 1)) Return True; } IF (a! = 0) { Expression [I] = '(' EXPB '/' EXPA ')'; Number [i] = b / A; IF (SEARCH (N - 1)) Return True; } Number [i] = a; Number [J] = B; Expression [I] = EXPA; Expression [J] = EXPB; } } Return False; } void main () { For (int i = 0; i INT X; CIN >> X; Number [i] = x; ITOA (X, Buffer, 10); Expression [I] = buffer; } IF (Search (count_of_number) { Cout << "Success." << Endl; } Else { COUT << "fail." << endl; } }