Solving the eight queens problem by using the Recursive Backtracking Method of javascript

Source: Internet
Author: User

Solving the eight queens problem by using the Recursive Backtracking Method of javascript

The Recursive Backtracking Method of javascript solves the eight queens problem:

I have seen many articles about the eight Queens algorithm on the internet, and I rarely see javascript. Today I will use javascript to solve this problem. If you need it, you can refer to it.

Next I will share with you the solution of the eight queens by the backtracing method, with detailed annotations. There is not much nonsense here.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

Function NQueens (order ){

If (order <4 ){

Console. log ('n' Queens problem apply for order bigger than 3! ');

Return;

}

 

Var nQueens = [];

Var backTracking = false;

RowLoop:

For (var row = 0; row <order; row ++ ){

// If row is smaller than 0, the problem is not solved.

If (row <0 ){

Console. log ('this N Queens problem has no solution! ');

Break;

}

// The first time a new row is detected

If (nQueens [row] === undefined ){

NQueens [row] = [];

}

// Program block that runs during backtracking

For (var col = 0; col <order; col ++ ){

// 0 indicates the location where the queen has been detected and can be placed

If (nQueens [row] [col] = 0 ){

Continue;

}

// In the Backtracking process, if the location can be placed in the Queen's position, it indicates that this location is not verified and needs to be re-processed.

Else if (backTracking & nQueens [row] [col] = 1 ){

// You can find that the last row ends at the end of the row, and you need to continue backtracking.

If (col = order-1 ){

ResetRow (nQueens, order, row );

Row = row-2;

Continue rowLoop;

}

// The row to be traced has not reached the end of the row. Mark 0 and continue.

NQueens [row] [col] = 0;

BackTracking = false;

Continue;

}

// Place a queen

NQueens [row] [col] = 1;

// Locate a location where the queen can be placed and jump out to the next line (only one queen can be placed on one row ).

If (isQueenValid (nQueens, row, col )){

Continue rowLoop;

}

// There should be a queen in each row. No proper position has been found at the end of the column, which indicates that there is a problem with the placement of the Queen in front. You need to trace back!

Else if (col = order-1 ){

BackTracking = true;

// Both 0 and 1 indicate that this position has been detected. Therefore, we need to clear this line as undefined.

ResetRow (nQueens, order, row );

// Subtract 2 because there is an auto-increment at the end of the loop, which is actually returned to the previous row.

Row = row-2;

// Return to the outer loop and continue

Continue rowLoop;

} Else {

// If the row is not found, continue to detect the row that has not been detected

NQueens [row] [col] = 0;

Continue;

};

}

}

Return nQueens;

}

// Clear the row before backtracking

Function resetRow (nQueens, order, row ){

For (var col = 0; col <order; col ++ ){

NQueens [row] [col] = undefined;

}

}

// Check whether the location can be placed as queen

Function isQueenValid (nQueens, row, col ){

// Row Detection

For (var I = 0; I <col; I ++ ){

If (nQueens [row] [I] = 1 ){

Return false;

}

}

For (var j = 1; j <row + 1; j ++ ){

// Column detection: Top left 45 degrees, top right 45 degrees

If (nQueens [row-j] [col] = 1 | (nQueens [row-j] [col-j] = 1) | (nQueens [row-j] [col + j] = 1 )){

Return false;

}

}

Return true;

}

 

Function printQ (queens ){

For (var row = 0; row <queens. length; row ++ ){

Var rowText = '';

For (var col = 0; col <queens. length; col ++ ){

If (queens [row] [col] === undefined ){

Queens [row] [col] = 0;

}

RowText = rowText + queens [row] [col] + '';

}

Console. log (rowText );

}

}

 

Var queens = NQueens (8 );

PrintQ (queens );

The above is all the content of this article. I hope you will like it.

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.