One of the algorithms: Aging Problems

Source: Internet
Author: User

There are several search algorithms:

  • Enumeration algorithm:
    • That is, to list all the statuses of the problem and find a solution that meets the problem.
    • It is suitable for problems with few States and relatively simple.
  • Extended search:
    • Start from the initial point, expand the first-level nodes according to the rules, and check whether the target nodes are on these nodes. If not, expand all the first-level nodes one by one, the second-level node is obtained. If not, the node is extended until the target node is found.
    • It is more suitable for the question of least step or minimum solution sequence.
    • Generally, a queue is set, the Start Node is put into the queue, and a node is retrieved from the queue header to check whether the node is the target node. If not, it is extended, put all the extended nodes at the end of the team, and then retrieve a node from the queue header until the target node is found.
  • Deep Priority Search:
    • Generally, a stack is set, the starting node is put into the stack, and a node pops up from the stack to check whether it is the target node. If not, it is extended, add all extended nodes to the stack, and then a node pops up from the top of the stack until the target node is found.
    • The first solution obtained by deep priority search is not necessarily the optimal solution.
  • Bidirectional breadth-first search:
    • Bidirectional search: searches from the Start Node to the target node, and searches from the target node to the Start Node.
    • Bidirectional search can only be used in breadth-first search.
    • The number of extended nodes in two-way search is smaller than that in one-way search.
  • A * Algorithm
    • Use the rules and features of the problem to develop some heuristic rules, so as to change the expansion sequence of nodes and expand the nodes with the most promising expansion of the optimal solution first, so as to find the optimal solution as soon as possible.
    • For each node, there is an evaluation function F to estimate the cost of the optimal path for the starting node to reach the target node through the node.
    • When each node is expanded, always select the node with the smallest F.
    • F = G + B × H: G is the actual cost from the Start Node to the current node. It has been calculated that H is the estimated cost of the optimal path from the node to the target node. F must increase monotonically.
    • B. It is best to change proportionally with the search depth. Where the search depth is low, the search mainly relies on the heuristic information to approach the target as soon as possible. When the search depth, gradually turn to breadth-first search.
  • Backtracking Algorithm:
    • Similar to the depth priority, the difference is that when you expand a node, not all the child nodes are extended, but only one of them. Therefore, it is blind, but the memory usage is small.
  • Optimization in Search:
    • Before a search, the search scale is reduced based on the search criteria.
    • In the extended search, processed nodes are fully released.
    • Pruning the data condition.
    • Use the intermediate solution in the search process to avoid repeated computation.

I. Horse's path

Question:

On a 4x5 board, the start coordinate of the horse is input by the user, and the horse returns the total number and steps of all different steps at the initial position. The path where the horse passes cannot be repeated.

 

// Chess. h

# Include <vector>
Using namespace std;

Class Chess
{
Public:
Chess (int w, int h): width (w), height (h)
{
Chess = new int * [w];
For (int I = 0; I <w; I ++ ){
Chess [I] = new int [h];
For (int j = 0; j Chess [I] [j] = 0;
}
}
}

Int FindPath (int startx, int starty );
Protected:
Private:

Const static int stepx [8];
Const static int stepy [8];

Int ** chess;
Int width;
Int height;

Struct Pos {
Pos (): x (0), y (0 ){}
Pos (int _ x, int _ y): x (_ x), y (_ y ){}
Int x;
Int y;
};

Pos startp;

Void jump (int posx, int posy, int & count, vector <Pos> & path );
};

// Chess. cpp

# Include "Chess. h"

Const int Chess: stepx [8] = {-2,-1, 1, 2, 2, 1,-1,-2 };
Const int Chess: stepy [8] = {1, 2, 2, 1,-1,-2,-2,-1 };

Int Chess: FindPath (int startx, int starty)
{
For (int I = 0; I <width; I ++)
For (int j = 0; j Chess [I] [j] = 0;
Startp. x = startx;
Startp. y = starty;
Vector <Pos> path;
Int count = 0;
Chess [startx] [starty] = 1;
Pos p (startx, starty );
Path. push_back (p );
Jump (startx, starty, count, path );
Return count;
}

Void Chess: jump (int posx, int posy, int & count, vector <Pos> & path)
{
For (int I = 0; I <8; I ++ ){
Int nextx = posx + stepx [I];
Int nexty = posy + stepy [I];
If (nextx> = 0 & nextx <width & nexty> = 0 & nexty Chess [nextx] [nexty] = 1;
Pos p (nextx, nexty );
Path. push_back (p );
Jump (nextx, nexty, count, path );
Path. pop_back ();
Chess [nextx] [nexty] = 0;
} Else if (nextx = startp. x & nexty = startp. y ){
Count ++;
Pos p (nextx, nexty );
Path. push_back (p );
Printf ("The % dth path:", count );
Vector <Pos>: iterator iter;
For (iter = path. begin (); iter! = Path. end (); iter ++ ){
Pos cur = * iter;
Printf ("(% d, % d)->", cur. x, cur. y );
}
Printf ("\ n ");
Path. pop_back ();
}
}
}

// Main

# Include "stdafx. h"
# Include "Chess. h"

Int _ tmain (int argc, _ TCHAR * argv [])
{
Chess ch (4, 5 );
Int count = ch. FindPath (1, 1 );
Printf ("The total number of path is: % d. \ n", count );
Return 0;
}

 

2. Two-layer Tower

Pandata is a toy derived from Indian mythology. When God created the world, he made three diamond pillars and stacked 64 gold disks from bottom to top. God ordered the Brahman to re-place the disc from below in order of size on another pillar. It is also stipulated that the disc cannot be enlarged on a small disc, and only one disc can be moved between the three pillars at a time.

In the simplest way, when there are only two plates, the moving process should be as follows:

When there are multiple plates, consider the other plates except the last one as a whole and apply recursive movement as follows:

Therefore, the following algorithm is obtained:

Void hanoi (int count, char pillarA, char pillarB, char pillarC)
{
If (count = 1 ){
Printf ("move from % c to % c. \ n", pillarA, pillarC );
} Else {
Hanoi (count-1, pillarA, pillarC, pillarB );
Printf ("move from % c to % c. \ n", pillarA, pillarC );
Hanoi (count-1, pillarB, pillarA, pillarC );
}
}

Double-Layer refer tower problem: the double-layer refer tower is derived from the single-layer refer tower. disks of the same size have two different colors. The purpose is to move the disks of different colors to the two columns on the right. In the process of moving, it is still observed that the dashboard must be under a small disk, and the color order is unlimited.

When there are only four dishes:

When there are multiple plates, consider the other plates except the last two as a whole and apply recursive movement as follows:

The implementation algorithm of the double-layer tower is as follows:

Void hanoi (int numofpairs, char pillarA, char pillarB, char pillarC)
{
If (numofpairs = 1)
{
Printf ("move from % c to % c. \ n", pillarA, pillarC );
Printf ("move from % c to % c. \ n", pillarA, pillarC );
}
Else
{
Hanoi (numofpairs-1, pillarA, pillarC, pillarB );
Printf ("move from % c to % c. \ n", pillarA, pillarC );
Printf ("move from % c to % c. \ n", pillarA, pillarC );
Hanoi (numofpairs-1, pillarB, pillarA, pillarC );
}
}
Void hanoi_double (int count, char pillarA, char pillarB, char pillarC)
{
For (int I = count/2; I> 1; I --)
{
Hanoi (I-1, pillarA, pillarB, pillarC );
Printf ("move from % c to % c. \ n", pillarA, pillarB );
Printf ("move from % c to % c. \ n", pillarA, pillarB );
Hanoi (I-1, pillarC, pillarB, pillarA );
Printf ("move from % c to % c. \ n", pillarB, pillarC );
}
Printf ("move from % c to % c. \ n", pillarA, pillarB );
Printf ("move from % c to % c. \ n", pillarA, pillarC );
}

Iii. Go to the chessboard

The principle of the server Guard is to take the Japanese character. The server guard starts from any position and asks him how to finish all the positions.

The steps of the server guard can be basically solved by recursion. However, the pure recursive dimension is large and inefficient. Therefore, the server guard does not choose the next step to apply the * algorithm, take the least steps that can be followed by the next step, that is, finish the most difficult position first.

// Knight. h

Class knight
{
Public:
Knight (int w, int h): width (w), height (h)
{
Board = new int * [width];
For (int I = 0; I <width; I ++)
{
Board [I] = new int [height];
For (int j = 0; j {
Board [I] [j] = 0;
}
}
}

~ Knight ()
{
For (int I = 0; I {
Delete [] board [I];
}
Delete [] board;
}

Int travel (int startx, int starty );

Void output ()
{
For (int I = 0; I <width; I ++)
{
For (int j = 0; j {
Printf ("% d \ t", board [I] [j]);
}
Printf ("\ n ");
}
}
Protected:
Private:
Int ** board;
Int width;
Int height;

Const static int stepx [8];
Const static int stepy [8];
};

// Knight. cpp

# Include "knight. h"
# Include <vector>
Using namespace std;

Const int knight: stepx [8] = {-2,-1, 1, 2, 2, 1,-1,-2 };
Const int knight: stepy [8] = {1, 2, 2, 1,-1,-2,-2,-1 };

Int knight: travel (int startx, int starty)
{
Board [startx] [starty] = 1;
Int totalsteps = width * height;

Vector <int> nextx;
Vector <int> nexty;
Vector <int> nextsteps;

Int currentx = startx;
Int currenty = starty;
For (int step = 2; step <= totalsteps; step ++)
{
Nextx. clear ();
Nexty. clear ();
Nextsteps. clear ();
For (int I = 0; I <8; I ++)
{
Int x = currentx + stepx [I];
Int y = currenty + stepy [I];
If (x> = 0 & x <width & y> = 0 & y {
Nextx. push_back (x );
Nexty. push_back (y );
}
}

Int selected = 0;
If (nextx. size () = 0)
{
Return 0;
}
Else if (nextx. size () = 1)
{
Selected = 0;
}
Else
{
Nextsteps. resize (nextx. size (), 0 );
For (int I = 0; I <nextx. size (); I ++)
{
For (int j = 0; j <8; j ++)
{
Int x = nextx [I] + stepx [j];
Int y = nexty [I] + stepy [j];
If (x> = 0 & x <width & y> = 0 & y {
Nextsteps [I] ++;
}
}
}

Int tmp = nextsteps [0];
Selected = 0;
For (int I = 0; I <nextsteps. size (); I ++)
{
If (nextsteps [I]> tmp)
{
Tmp = nextsteps [I];
Selected = I;
}
}
}

Currentx = nextx [selected];
Currenty = nexty [selected];
Board [currentx] [currenty] = step;
}
}

Int main (int argc, char ** argv ){

Knight k (3, 3 );
K. travel (0, 0 );
K. output ();
Return 0;
}

4. The mouse goes through the maze, and the mouse traverses the maze

The mouse goes through the maze. In the two-dimensional matrix, 2 represents the wall, 1 represents the walking path of the mouse, and the path from the entrance to the exit is obtained.

The mouse moves in four directions: Up, down, and up and down. Each time you choose to move in one direction, when you cannot move forward, return to the next direction until you exit.

Due to the design of the maze, there may be more than one path from the mouse's entrance to the exit. How can we find all the paths?

It is actually very easy to find all the paths. As long as the mouse does not exit when it reaches the exit, it only displays the path passed by, and then returns to the previous one and selects the next location.

Class MouseMaze
{
Public:
MouseMaze (int * maze, int width, int height, int startx, int starty, int endx, int endy)
{
This-> maze = maze;
This-> width = width;
This-> height = height;
This-> startx = startx;
This-> starty = starty;
This-> endx = endx;
This-> endy = endy;
}

Int visit (int x, int y)
{
Maze [x * width + y] = 1;
If (x = endx & y = endy)
{
Output ();
Return TRUE;
}

Int success = FALSE;
If (success = FALSE & x + 1 <width & maze [(x + 1) * width + y] = 0)
{
Success = visit (x + 1, y );
}
If (success = FALSE & x-1> = 0 & maze [(x-1) * width + y] = 0)
{
Success = visit (x-1, y );
}
If (success = FALSE & y + 1 {
Success = visit (x, y + 1 );
}
If (success = FALSE & y-1> = 0 & maze [x * width + Y-1] = 0)
{
Success = visit (x, y-1 );
}

If (success = FALSE)
{
Maze [x * width + y] = 0;
}

Return success;
}

Void visitAll (int x, int y)
{
Maze [x * width + y] = 1;
If (x = endx & y = endy)
{
Output ();
Maze [x * width + y] = 0;
Return;
}

If (x + 1 <width & maze [(x + 1) * width + y] = 0)
{
VisitAll (x + 1, y );
}
If (x-1> = 0 & maze [(x-1) * width + y] = 0)
{
VisitAll (x-1, y );
}
If (y + 1 {
VisitAll (x, y + 1 );
}
If (Y-1> = 0 & maze [x * width + Y-1] = 0)
{
VisitAll (x, y-1 );
}

Maze [x * width + y] = 0;
}

Void output ()
{
If (maze = NULL)
{
Return;
}

For (int I = 0; I <width; I ++)
{
For (int j = 0; j {
Printf ("% d \ t", maze [I * width + j]);
}
Printf ("\ n ");
}

Printf ("------------------------------------------------------------- \ n ");
}

Protected:
Private:
Int * maze;
Int width;
Int height;
Int startx;
Int starty;
Int endx;
Int endy;
};

Int _ tmain (int argc, _ TCHAR * argv [])
{
Int maze [9] [9] = {2, 2, 2, 2, 2, 2, 2, 2 },
{2, 0, 0, 0, 0, 0, 0, 0, 2 },
{2, 0, 2, 2, 0, 2, 2, 0, 2 },
{2, 0, 2, 0, 0, 2, 0, 0, 2 },
{2, 0, 2, 0, 2, 0, 2, 0, 2 },
{2, 0, 0, 0, 0, 0, 2, 0, 2 },
{2, 2, 0, 2, 2, 0, 2, 2 },
{2, 0, 0, 0, 0, 0, 0, 0, 2 },
{2, 2, 2, 2, 2, 2, 2, 2, 2 }};

MouseMaze m (& maze [0] [0], 9, 9, 1, 1, 7, 7 );
M. visitAll (1, 1 );
Return 0;
}

V. Three-Color Flag Problems

Assume that there is a rope with red, white, and blue flags on it. At first, the flags on the rope were not in order and should be arranged in the order of blue, white, and red, how to move is the least possible. Only two flags can be exchanged at a time.

Set a queue where B, W, and R are the demarcation points of the three flags. Initially, B, W are at the front of the team, R is at the end of the team, and then scan one by one from the front of the team:

  • If it is white, W + 1 indicates that the unprocessed part is moved to the white group.
  • If it is blue, the elements of B and W are reversed. B + 1, W + 1 indicate that one element is added to both groups.
  • If the color is red, the R and W elements are reversed, indicating that the red color has one more element.

Void swap (char & a, char & B)
{
Char tmp =;
A = B;
B = tmp;
}

Void colorflags (char * & flags, int length)
{
Int wflag = 0;
Int bflag = 0;
Int rflag = length-1;
While (wflag <= rflag)
{
If (flags [wflag] = 'W ')
{
Wflag ++;
}
Else if (flags [wflag] = 'B ')
{
Swap (flags [bflag], flags [wflag]);
Bflag ++;
Wflag ++;
}
Else if (flags [wflag] = 'R ')
{
While (wflag <rflag & flags [rflag] = 'R ')
{
Rflag --;
}
Swap (flags [wflag], flags [rflag]);
Rflag --;
}
}
}

 

6. Eight queens

In chess, the queen can go straight, vertical, and oblique, and eat all the pieces. If there are eight queens on the board, how can they be placed to make the eight queens safe.

Class Queen
{
Public:
Queen (int n, int l): number (n), length (l)
{
Chess = new int * [length];
For (int I = 0; I <length; I ++)
{
Chess [I] = new int [length];
For (int j = 0; j <length; j ++)
{
Chess [I] [j] = 0;
}
}
Column. resize (number, 0 );
Upright. resize (2 * number, 0 );
Downright. resize (2 * number, 0 );
}

~ Queen ()
{
For (int I = 0; I <length; I ++)
{
Delete [] chess [I];
}
Delete [] chess;
}

Void output ()
{
For (int I = 0; I <length; I ++)
{
For (int j = 0; j <length; j ++)
{
Printf ("% d \ t", chess [I] [j]);
}
Printf ("\ n ");
}
Printf ("---------------------------------------------------- \ n ");
}

Void placeQueen (int I)
{
If (I> = number)
{
Output ();
}
Else
{
For (int j = 0; j <length; j ++)
{
If (column [j] = 0 & upright [I + j] = 0 & downright [I-j + number] = 0)
{
Chess [I] [j] = 1;
Column [j] = upright [I + j] = downright [I-j + number] = 1;
PlaceQueen (I + 1 );
Chess [I] [j] = 0;
Column [j] = upright [I + j] = downright [I-j + number] = 0;
}
}
}
}
Protected:
Private:
Int number;
Int length;
Int ** chess;
Vector <int> column;
Vector <int> upright;
Vector <int> downright;
};

Int _ tmain (int argc, _ TCHAR * argv [])
{
Queen q (8, 8 );
Q. placeQueen (0 );
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.