Depth search DFS!

Well, the next step is this adorable new first blog friends.
Directly on deep search !
Depth-first search (Depth-First-Search), referred to as "deep search" (dfs), we konjac are the most basic one of the search operation.
Simply put, deep search is recursive.
The following explanation is copied:
a depth-first search of all stored state with a generated array.
(1) into the initial state of the array, as the current state;
(2) extend the current state, to produce a new state into the array while the state of the current status of the newly generated set;
(3) determines the current and repeating the foregoing state is, if a state is repeated on the back, to produce its other state;
(4) determines whether a current state is a goal state if the goal is to find a solution, the algorithm ends.
(5) If the array is empty, indicating that no solution.
If you can not read, oh, yes, you can continue to imagine what will happen. (? Of course, if you read, big brother thing we can be friends)
Here is an illustration:
if we need to go from A to G to the solid line represents that we can take.
So how can we go?
In this case, we can only from A> B> C> D> E> F> G this road to go, which is not redundant method, it is very simple.
But this situation we can have a variety of options out from A. And we do not know how to go to reach the G-spot, then we choose the road we did not have to go through, so it is possible to arrive G.

For example, we can reach B, C, D from the three points A, E point which we choose.
When we go from E A, but also to the B, C, D, F four points, so we go to point B.
When we arrived after B, we found three roads, pointing to A, C, E three points.
Because we do not turn back ! I can only come to the point C.
When we went after C, have access to B, E, G road three points, we can go directly to the G-spot.
 
In view of this , "come before does not take the old road" approach is to find a way until you come to the end point.
While this may be "the long way" , but we just to reach the target "not the fastest," and can "Xianxin walk" when this a good choice.
(Liver iron head all the way in the end !!!!!)
which is found deep Oh!
The following is a deep search ideas:
1. All points marked as "not come"; // the whole array of other labeled 0 or
2. Find the start, end and see where you can go; // preparation cycle
3. Choose determines whether a present node and the node map out; //
4. Analyzing the node did not come ah; // 3,4-step is to determine whether the node is valid go to
5. If it did not come into the node; // mark the node
6 and then find another node; // one level deeper search at
7. run out you can get back to the // return which can back up

And then add a deep search topic :( Taken Los Eight Queens Valley P1219 // actually comes from USACO of!)
P1219 eight Queen
Title Description
check following a checkerboard 6 x 6, and six pieces were placed on the board, such that each row, each column and only one, each diagonal (including two main diagonal at most one piece on all parallel lines).
The above sequence can be layout 246 135 described, it denotes the i-th digit has a piece in the corresponding position of the i-th row, as follows:
Line No. 123 456
Column No. 246135
It's just checkers placed a solution. Please compile a program to find solutions to place all the checkers. Sequence and outputs them to the above method. Solutions lexicographically arranged. Please output of the previous three solutions. The last line is the total number of solutions.
// the following words from usaco official, does not represent the views Luo Valley
Special Note: For larger N (board size N x N) your program should be improved more effectively. Do not all solutions are computed in advance and then only output (or find a formula about it), this is cheating. If you insist on cheating, then you USACO Training login account deleted and can not participate in any race USACO of. I warned you!
Input format
a number N (6 <= N <= 13) is an N x N represents the board size.
Output Format
The first three solutions before three, separated by a space between the two numbers of each solution. The fourth line is only one number, the total number of Solutions.
Sample Input Output
Input # 1 
. 6
Output # 1
2. 5. 4. 3. 1. 6
. 3 2. 6. 1. 4. 5
. 4. 3. 1. 5 2. 6
. 4
Instructions / tips
subject translation from NOCOW.
USACO Training Section 1.5
 
 
The following is a savory source Oh!
Source:
#include <bits / STDC ++ H.> // live just universal header;
the using namespace STD; // not be construed by reference;
int H [15], L [15], ZX [30], YX [30] , N, cnt;
// == H line, l == column, zx == slash, rx == right diagonal, N being the size of the board, cnt record number;
void DFS (int X) // Search depth of course, it is dfs
{
 iF (the X-> N) // when filled with every line of the board of course, put over!
 {
  ++ CNT;
  IF (CNT <=. 3)
  {
   for (int I =. 1; I <= N; I ++)
    COUT << H [I] << ''; // output
   cout << endl; // remember clean the cache outside the loop
  }
 }
 the else // not done and continues to roll down ah just
 for (int I =. 1; I <= N; I ++)
  {
   IF (L [I] || ZX [I-X + N] YX || [I + X])
// if the same column of the lattice, with the left oblique, with a piece (labeled 1) on the right with the swash
    continue; // try the next on a grid 
   l [i] = zx [x -i + N] = yx [i + x] = 1; // not to mark marked thing
   h [x] = i; // save the
   dfs (x + 1); / / filter next line
   l [i] = zx [x -i + N] = yx [i + x] = 0; // Origin  
  }  
 }
 int main () // main function good short ah. . . .
 {
  CIN >> N;
 DFS (. 1); // just start searching for the first row of
 COUT << CNT;
 return 0; // the OK knock
 }
 
Well, this adorable new new first blog on here it!

Guess you like

Origin www.cnblogs.com/xrs-2019/p/11620914.html