Learning Data Structure Notes=====>Recursion

Learning Sources ->Portal -> Silicon Valley Java Data Structure and Java Algorithms (Java Data Structure and Algorithms)

Articles recommending this gangster; Master Recursion - > What is recursion, let's take a look at recursion through this article

1. Simple recursive cases

Recursively, calls itself within the method itself, but passes parameters differently; After the call, go to the end and step by step return the result to the location of the call.

```public class Demo {
public static void main(String[] args) {
getNum(5);
}

//Simple recursive cases;
public  static void getNum(int n){
if(n > 2){
getNum(n - 1);
}
System.out.println("n-->"+ n);
}
}
```

Get results

```n-->2
n-->3
n-->4
n-->5
```

Diagram of simulation process

Specific calling process;

• main() , getNum(5)
• At this time n=5; getNum(5) condition judgment --> getNum (4);
• At this time n=4; getNum(4) condition judgment --> getNum (3);
• At this time n=3; getNum(3) condition judgment --> getNum (2);
• At this time n=2; getNum(2) conditional judgment --> Sout output n=2;
• Return; sout output n=3;
• Return; sout output n=4;
• Return; sout output n=5;
• Final result 2; 3; 4; 5;

However, if an else {} is added to the execution condition; Then the result of execution is only one

```public class Demo {
public static void main(String[] args) {
getNum2(5);
}

//Simple recursion case 2;
public  static void getNum2(int n){
if(n > 2){
getNum2(n - 1);
}
//If an else statement is added to it;
else {
System.out.println("n-->"+ n);
}
}
}
```

results of enforcement

```n-->2
```

Diagram of process simulation;
When the result of the call returns and the condition (n>2) is found to be met, he will not take another path.

If an else statement is added; On return, the sout output is no longer executed because it does not meet the criteria

• main() , getNum(5)
• At this time n=5; getNum(5) condition judgment --> getNum (4);
• At this time n=4; getNum(4) condition judgment --> getNum (3);
• At this time n=3; getNum(3) condition judgment --> getNum (2);
• At this time n=2; getNum(2) conditional judgment --> Sout output n=2;
• Return;
• Return;
• Return;
• Final result 2;

The factorial case;

```public class Demo {
public static void main(String[] args) {
//Factorial call;
int i = factorial(5);
//System.out.println(i);
}

//Factorial recursion;
public static int factorial(int num){
return num == 1 ? 1 :num*factorial(num-1);
}
}
```

Illustrations do not draw any more;
The specific process is ->

• main() ; factorial(5);
• num =5; factorial(5) --> 5 * factorial(4)
• num =4; factorial(4) --> 4 * factorial(3)
• num =3; factorial(3) --> 3 * factorial(2)
• num =2; factorial(2) --> 2 * factorial(1)
• Num =1; Factorial(1) -->get 1 and execute up;
• num =2; 2*factorial(1) - >get 2 and execute up;
• num =3; 3*factorial(2) - get 6 and execute up;
• num =4; 4*factorial(3) ->24 followed by execution up;
• num =5; 5*factorial(4) - >120 to get the final result of 120;

2. Labyrinth problems

There is a small ball going from black to diagonal

0 -> indicates a missed point; 1 -> means fence (cannot pass); 2->indicates a possible point to walk through; 3 -> indicates the infeasible point to walk through;

This requires the use of retrospective methods; As the ball explores forward, it needs to change direction when it encounters a fence.
The following is going in the following direction - > Right - > Up - > Left;

```public class MazeDemo {
public static void main(String[] args) {

//Define a two-dimensional array as a maze;
int[][] mazeArray = new int[8][7];
//Provisions: 0 -> indicates a point that has not been moved; 1 -> means fence; 2->indicates a possible point to walk through; 3 -> indicates the infeasible point to walk through;
//Initialize the construction of the maze first; Use fences from top to bottom, left to right; And a few special points in the middle as the fence;
for (int i = 0; i < 7; i++) {
mazeArray[0][i] = 1;
mazeArray[7][i] = 1;
}
for (int i = 0; i < 8; i++) {
mazeArray[i][0] = 1;
mazeArray[i][6] = 1;
}
//Wall at Other Points
mazeArray[2][1] = 1;
mazeArray[2][2] = 1;
mazeArray[3][1] = 1;
mazeArray[3][2] = 1;
mazeArray[3][3] = 1;
mazeArray[3][4] = 1;

//Output to view the original maze;
System.out.println("Pre-Pass Labyrinth===>");
for (int[] ints : mazeArray) {
for (int i : ints) {
System.out.print(i + "\t");
}
System.out.println();
}

//Call method 1 to execute;
System.out.println(getSWay1(mazeArray, 1, 1));
System.out.println("Lower Right Upper Left==>A post-passage maze===>");
for (int[] ints : mazeArray) {
for (int i : ints) {
System.out.print(i + "\t");
}
System.out.println();
}
}

/**
* Find a path; Lower Right Upper Left
* @param mazeArray Simulated maze
* @param i         Starting point x-coordinate
* @param j         Starting Point ordinate
* @return Is it possible
*/
public static boolean getSWay1(int[][] mazeArray, int i, int j) {
//Define the end point;
if (mazeArray[6][5] == 2) {
return true;
}
//If the point position is 0, it is the initial point.
if (mazeArray[i][j] == 0) {
//Assume that the point location obtained is a feasible point; You can move in four directions, down->right->up->left; If not, mark this point as infeasible;
mazeArray[i][j] = 2;
if (mazeArray[i + 1][j] == 0) {
getSWay1(mazeArray,i+1, j);
return true;
} else if (mazeArray[i][j + 1] == 0) {
getSWay1(mazeArray,i, j+1);
return true;
} else if (mazeArray[i - 1][j] == 0) {
getSWay1(mazeArray,i-1, j);
return true;
} else if (mazeArray[i][j - 1] == 0) {
getSWay1(mazeArray,i, j-1);
return true;
} else {
mazeArray[i][j] = 3;
return false;
}
} else {
//The rest of the points will not pass directly.
return false;
}
}
}
```

implement

```Pre-Pass Labyrinth===>
1	1	1	1	1	1	1
1	0	0	0	0	0	1
1	1	1	0	0	0	1
1	1	1	1	1	0	1
1	0	0	0	0	0	1
1	0	0	0	0	0	1
1	0	0	0	0	0	1
1	1	1	1	1	1	1
true
Lower Right Upper Left==>A post-passage maze===>
1	1	1	1	1	1	1
1	2	2	2	0	0	1
1	1	1	2	2	2	1
1	1	1	1	1	2	1
1	0	0	0	0	2	1
1	0	0	0	0	2	1
1	0	0	0	0	2	1
1	1	1	1	1	1	1
```

Of course, other directions can also be used;
For example: top->right->bottom->left

```public class MazeDemo {
public static void main(String[] args) {

//Define a two-dimensional array as a maze;
int[][] mazeArray = new int[8][7];
//Provisions: 0 -> indicates a point that has not been moved; 1 -> means fence; 2->indicates a possible point to walk through; 3 -> indicates the infeasible point to walk through;
//Initialize the construction of the maze first; Use fences from top to bottom, left to right; And a few special points in the middle as the fence;
for (int i = 0; i < 7; i++) {
mazeArray[0][i] = 1;
mazeArray[7][i] = 1;
}
for (int i = 0; i < 8; i++) {
mazeArray[i][0] = 1;
mazeArray[i][6] = 1;
}
//Other fences
mazeArray[2][1] = 1;
mazeArray[2][2] = 1;
mazeArray[3][1] = 1;
mazeArray[3][2] = 1;
mazeArray[3][3] = 1;
mazeArray[3][4] = 1;

//Output to view the original maze;
System.out.println("Pre-Pass Labyrinth===>");
for (int[] ints : mazeArray) {
for (int i : ints) {
System.out.print(i + "\t");
}
System.out.println();
}

//Execution mode 2;
boolean result = getSWay2(mazeArray, 1, 1);
System.out.println(result);
System.out.println("Upper Right Lower Left==>A post-passage maze===>");
for (int[] ints : mazeArray) {
for (int i : ints) {
System.out.print(i + "\t");
}
System.out.println();
}
}

/**
* Find Path Up Right Down Left
* @param mazeArray Simulated maze
* @param i  Starting position x-coordinate
* @param j  Starting position ordinate
* @return
*/
public static boolean getSWay2(int[][] mazeArray, int i, int j) {
//Define the end point;
if (mazeArray[6][5] == 2) {
return true;
}
//If the point position is 0, it is the initial point.
if (mazeArray[i][j] == 0) {
//Assume that the point location obtained is a feasible point; You can move in four directions, up->right->down->left; If not, mark this point as infeasible;
mazeArray[i][j] = 2;
if (getSWay2(mazeArray,i-1, j)) {
return true;
} else if (getSWay2(mazeArray,i, j+1)) {
return true;
} else if (getSWay2(mazeArray,i+1, j)) {
return true;
} else if (getSWay2(mazeArray,i, j-1)) {
return true;
} else {
mazeArray[i][j] = 3;
return false;
}
} else {
//The rest of the points will not pass directly.
return false;
}
}
}
```

Execution path

```Pre-Pass Labyrinth===>
1	1	1	1	1	1	1
1	0	0	0	0	0	1
1	1	1	0	0	0	1
1	1	1	1	1	0	1
1	0	0	0	0	0	1
1	0	0	0	0	0	1
1	0	0	0	0	0	1
1	1	1	1	1	1	1
true
Upper Right Lower Left==>A post-passage maze===>
1	1	1	1	1	1	1
1	2	2	2	2	2	1
1	1	1	0	0	2	1
1	1	1	1	1	2	1
1	0	0	0	0	2	1
1	0	0	0	0	2	1
1	0	0	0	0	2	1
1	1	1	1	1	1	1
```

3.Queens of Eight Queens

The questions are as follows: × Place eight queens on an eight-grid chess board so that they can't attack each other, that is, no two Queens can be in the same row, column or slash. Ask how many different poses there are. Gauss sees 76 options. In 1854, 40 different solutions were published by different authors in the Berlin chess magazine, and later 92 results were solved using graph theory. If the pendulum is considered to be one type after (+) 90 degrees, (+) 180 degrees rotation and diagonal symmetric transformation, there are 42 types. After the computer was invented, there are many computer languages that can be programmed to solve this problem.

This solution uses a one-dimensional array to represent the location of each queen. The subscript index of the array acts as the row of the queen (zero-based);
The element value of the array as the queen's column position

```public class EightQueens {
public static void main(String[] args) {
EightQueens eq = new EightQueens();
//From the first queen; 0;
System.out.println("The total solution is->"+count);
}
//When defining, consider using a one-dimensional array to solve the problem. The index subscript of the array represents the row (zero-based); The elements of the array represent the current queen's column.
int queen = 8;
int[] queenArray = new int[queen];
//Solutions to statistical problems;
static int count = 0;

//The method of outputting the queen;
private  void getQueenArray(){
//Record the number of calls;
count ++;
for (int j : queenArray) {
System.out.print(j + "\t");
}
System.out.println();
}

//When adding a queen, decide if there is a conflict problem; q: denotes the number of queens;
private boolean isConflict(int q){
for (int i = 0; i < q; i++) {
//Not in the same column; And not diagonally spaced;
if(queenArray[q]==queenArray[i] || Math.abs(q-i)==Math.abs(queenArray[q]-queenArray[i])){
return false;
}
}
return true;
}

if(num == queen){
getQueenArray();
return;
}
for (int i = 0; i < queen; i++) {