The backtracking method is also called the testing method.ProblemThe limitation of the size of the problem, and the solution of the problem is enumerated and tested one by one in a certain order. In the Backtracking Method, the process of dropping the current half solution is called backtracking.
This example uses the Backtracking Method to output n natural numbers in a full arrangement of the numbers of R.CodeAs follows:
1 Public Void Arrange ( Int N, Int R)
2 Int I = 0 , J;
3 String S;
4 Int [] A = New Int [N];
5 A = 1 ;
6 While ( True )
7 {
8 If (A-I) <= (n-R + 1 ))
9 {
10 If (I = (R- 1 ))
11 {
12 S = "" ;
13 For (J = 0 ; J <r; j ++)
14 {
15 S = S + convert. tostring (A [J]) + " , " ;
16 }
17 // Memo1.lines. append (TRIM (s ));
18 Console. writeline (s );
19 A = a + 1 ;
20 Continue ;
21 }
22 I = I + 1 ;
23 A = A [I- 1 ] + 1 ;
24 }
25 Else
26 {
27 If (I = 0 )
28 {
29 Break ;
30 }
31 I = I- 1 ;
32 A = a + 1 ;
33 }
34 }