This article is to introduce the two recursive demo (maze and eight queens)
/ ** * @desc maze * 8 1. Initialize a matrix of seven rows of Map [8] [. 7] * 2. Assumptions: * (1) of three types: 0-not gone, 1 baffle, can take 2- (has gone), 3- nowhere * (2) policy: under - "right -" lower - "left * (3) initial value: 1 surrounded * 3. process analysis: * (1) It has been found (OK locate conditions: line 7 = 6 shall find "[6,5] = 2) * (2) has not been found by policy to look for * * @author XW * @date 2019/9/3 * / public class Minggong { public static void main (String [] args) { // initialize a matrix arr 1. 8 rows 7 of [8] [. 7] int [] [] = Map new new int [8] [. 7 ]; // 2 initial value: 1 surrounded // row 1, line 8 and the baffle is provided for (int J = 0; J <. 7; J ++) { Map [0] [J] = 1; Map [. 7] [J] =. 1; } // Column 1 and Column baffle 7 is provided for (int I = 0; I <. 8; I ++) { Map [I] [0] =. 1; Map [I] [. 6] =. 1; } // Output Matrix Print (Map); System.out.println ( "---------"); // set the tailgate [3,1] =. 1, [3,2-] =. 1 Map [. 3] [. 1] =. 1; Map [. 3] [2] =. 1; Print (Map); // policy: under - "right -" on - "left setWay (map, 1, 1) ; // from [1,1] the start position System.out.println ( "policy: under -" right - "on -" left => "); Print (Map); } }
/ ** * @desc 8 Queens * 1. What is? * 8x8 matrix, any two locations can not be in the same line, same column or the same diagonal, to find out how many kinds of solution, which is 8 Queens * 2. thinking analysis * (1) put the first line of the first Queen The first column [0,0] * (2) the first column of the second discharge Queen [1,0] ... until the second row is determined ok, * if not ok, continue in the second column, third column turn to put all the columns finished, find a suitable location * (3) continue to a third queen, or the first column, second row ... until eight queens also in a position that does not conflict, * be found a correct solution * (4) when the time to get a correct solution, when a stack on the stack to fall back, will begin to backtrack, the upcoming first queen, put all the correct solution of the first column, all get * (5 ) and then back to continue with Queen put a second row, behind the loop continues to perform steps 1-4 * 3. Description * theoretically create a two-dimensional array to represent a chessboard, but in fact can algorithmically, * with a one-dimensional the array can be solved ARR [. 8] = {0,. 4,. 7,. 5, 2,. 6,. 1,. 3} * // ARR [0] = 0 denotes a queen (first row Columns 1, i.e. [0,0] * // ARR [1] = 2. 4 represents the queen (second row) in the fifth column, i.e., [1,4] * subscript denotes the first few lines i.e. the first few Queen * arr [i] = val, val i + 1 represents the Queen val + 1 on the first column of row i + 1 * @author XW * @date 2019/9/4 * / public class Queen8 { private int max; private int[] arr; private int count; public Queen8(int max) { this.max = max; this.arr = new int[max]; } public static void main(String[] args) { for (int i = 0; i < 8; i++) { System.out.print(String.format("R%s\t", i+1)); } System.out.println(); Queen8 queen8 = new Queen8(8); queen8.check(0); System.out.println(String.format("共有%s种解法", queen8.count)); } }
More data structure Case venue https://line007.github.io/2019/10/28/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84% E4% B8% 8E% E7% AE% 97% E6% B3% 95 / # more