PHP uses the backtracking algorithm to solve the n queen problem.

Source: Internet
Author: User

PHP uses the backtracking algorithm to solve the n queen problem.

This article describes how PHP solves the n queen problem based on the backtracking algorithm. We will share this with you for your reference. The details are as follows:

I will not introduce n queen's problem too much here. For the introduction and algorithm analysis, refer to the previous article C ++ to solve the eight Queen's Problem Based on backtracking.

The basic method of backtracking is to search, or a well-organized search method that can avoid unnecessary search. This method is suitable for solving problems with a large number of combinations.

In the solution space tree of a problem, the Backtracking Method searches for the solution space tree from the root node based on the depth priority policy. When the algorithm searches for any point in the solution space tree, it first determines whether the node contains the solution of the problem. If it is not included, search for the subtree with the root node is skipped and trace back to its ancestor node layer by layer. Otherwise, search for the subtree Based on the deep priority policy.

The guiding ideology of the Backtracking Method-if you cannot get through, you will turn around. Design Process: Determine the problem solution space, determine the node extension rules, and search.

This section describes how to use php to solve this problem.

$ Tres indicates a feasible attempt.
$ Res record summary result

For detailed data structure analysis, refer to the previous article link.

<?php//check is valid nowfunction check($l,$c){   global $tres;   global $res;   global $n,$count;   foreach($tres as $key=>$value){     if($key<$l){     if($value==$c){       return 0;     }else if(abs($l-$key)==abs($c-$value)){      return 0;     }    }   }   return 1;}function scan($line){   global $tres;   global $res;   global $n,$count;   if($line==$n){     $res[]=$tres;    // $tres=array();    $count++;   }else{     for($i=0;$i<$n;$i++){       if(check($line,$i)==1){        $tres[$line]=$i;        $nxline=$line+1;        scan($nxline);       }     }   }}$tres=array();$res=array();$n=8;$count=0;$stime=microtime();scan(0);$etime=microtime();var_dump($res);echo "there is $count ways !\n";$t=$etime-$stime;echo (int)$t."seconds used.";

Running result:
Copy codeThe Code is as follows:
Array (92) {[0] => array (8) {[0] => int (0) [1] => int (4) [2] => int (7) [3] => int (5) [4] => int (2) [5] => int (6) [6] => int (1) [7] => int (3)} [1] => array (8) {[0] => int (0) [1] => int (5) [2] => int (7) [3] => int (2) [4] => int (6) [5] => int (3) [6] => int (1) [7] => int (4)} [2] => array (8) {[0] => int (0) [1] => int (6) [2] => int (3) [3] => int (5) [4] => int (7) [5] => int (1) [6] => int (4) [7] => int (2 )} [3] => array (8) {[0] => Int (0) [1] => int (6) [2] => int (4) [3] => int (7) [4] => int (1) [5] => int (3) [6] => int (5) [7] => int (2 )} [4] => array (8) {[0] => int (1) [1] => int (3) [2] => int (5) [3] => int (7) [4] => int (2) [5] => int (0) [6] => int (6) [7] => int (4)} [5] => array (8) {[0] => int (1) [1] => int (4) [2] => int (6) [3] => int (0) [4] => int (2) [5] => int (7) [6] => int (5) [7] => int (3)} [6] => array (8) {[0] => int (1) [1] => int (4) [2] => in T (6) [3] => int (3) [4] => int (0) [5] => int (7) [6] => int (5) [7] => int (2)} [7] => array (8) {[0] => int (1) [1] => int (5) [2] => int (0) [3] => int (6) [4] => int (3) [5] => int (7) [6] => int (2) [7] => int (4)} [8] => array (8) {[0] => int (1) [1] => int (5) [2] => int (7) [3] => int (2) [4] => int (0) [5] => int (3) [6] => int (6) [7] => int (4)} [9] => array (8) {[0] => int (1) [1] => int (6) [2] => int (2) [3] => int (5) [4] => int (7) [5] => int (4) [6] => int (0) [7] => int (3)} [10] => array (8) {[0] => int (1) [1] => int (6) [2] => int (4) [3] => int (7) [4] => int (0) [5] => int (3) [6] => int (5) [7] => int (2 )} [11] => array (8) {[0] => int (1) [1] => int (7) [2] => int (5) [3] => int (0) [4] => int (2) [5] => int (4) [6] => int (6) [7] => int (3)} [12] => array (8) {[0] => int (2) [1] => int (0) [2] => int (6) [3] => int (4) [4] => int (7) [5] => int (1) [6] => int (3) [7] => Int (5)} [13] => array (8) {[0] => int (2) [1] => int (4) [2] => int (1) [3] => int (7) [4] => int (0) [5] => int (6) [6] => int (3) [7] => int (5)} [14] => array (8) {[0] => int (2) [1] => int (4) [2] => int (1) [3] => int (7) [4] => int (5) [5] => int (3) [6] => int (6) [7] => int (0)} [15] => array (8) {[0] => int (2) [1] => int (4) [2] => int (6) [3] => int (0) [4] => int (3) [5] => int (1) [6] => int (7) [7] => int (5 )} [16] => array (8) {[0] => int (2) [1] => int (4) [2] => int (7) [3] => int (3) [4] => int (0) [5] => int (6) [6] => int (1) [7] => int (5 )} [17] => array (8) {[0] => int (2) [1] => int (5) [2] => int (1) [3] => int (4) [4] => int (7) [5] => int (0) [6] => int (6) [7] => int (3)} [18] => array (8) {[0] => int (2) [1] => int (5) [2] => int (1) [3] => int (6) [4] => int (0) [5] => int (3) [6] => int (7) [7] => int (4)} [19] => array (8) {[0] => int (2) [1] => int (5) [2] => int (1) [3] => int (6) [4] => int (4) [5] => int (0) [6] => int (7) [7] => int (3)} [20] => array (8) {[0] => int (2) [1] => int (5) [2] => int (3) [3] => int (0) [4] => int (7) [5] => int (4) [6] => int (6) [7] => int (1)} [21] => array (8) {[0] => int (2) [1] => int (5) [2] => int (3) [3] => int (1) [4] => int (7) [5] => int (4) [6] => int (6) [7] => int (0 )} [22] => array (8) {[0] => int (2) [1] => int (5) [2] => int (7) [3] => int (0 )[ 4] => int (3) [5] => int (6) [6] => int (4) [7] => int (1 )} [23] => array (8) {[0] => int (2) [1] => int (5) [2] => int (7) [3] => int (0) [4] => int (4) [5] => int (6) [6] => int (1) [7] => int (3)} [24] => array (8) {[0] => int (2) [1] => int (5) [2] => int (7) [3] => int (1) [4] => int (3) [5] => int (0) [6] => int (6) [7] => int (4)} [25] => array (8) {[0] => int (2) [1] => int (6) [2] => int (1) [3] => int (7) [4] => int (4) [5] => int (0) [6] => Int (3) [7] => int (5)} [26] => array (8) {[0] => int (2) [1] => int (6) [2] => int (1) [3] => int (7) [4] => int (5) [5] => int (3) [6] => int (0) [7] => int (4)} [27] => array (8) {[0] => int (2) [1] => int (7) [2] => int (3) [3] => int (6) [4] => int (0) [5] => int (5) [6] => int (1) [7] => int (4 )} [28] => array (8) {[0] => int (3) [1] => int (0) [2] => int (4) [3] => int (7) [4] => int (1) [5] => int (6) [6] => int (2) [7] => int (5)} [29] => Array (8) {[0] => int (3) [1] => int (0) [2] => int (4) [3] => int (7) [4] => int (5) [5] => int (2) [6] => int (6) [7] => int (1)} [30] => array (8) {[0] => int (3) [1] => int (1) [2] => int (4) [3] => int (7) [4] => int (5) [5] => int (0) [6] => int (2) [7] => int (6)} [31] => array (8) {[0] => int (3) [1] => int (1) [2] => int (6) [3] => int (2) [4] => int (5) [5] => int (7) [6] => int (0) [7] => int (4)} [32] => array (8) {[0] => int (3) [1] => int (1) [2] => int (6) [3] => int (2) [4] => int (5) [5] => int (7) [6] => int (4) [7] => int (0)} [33] => array (8) {[0] => int (3) [1] => int (1) [2] => int (6) [3] => int (4) [4] => int (0) [5] => int (7) [6] => int (5) [7] => int (2 )} [34] => array (8) {[0] => int (3) [1] => int (1) [2] => int (7) [3] => int (4) [4] => int (6) [5] => int (0) [6] => int (2) [7] => int (5)} [35] => array (8) {[0] => int (3) [1] => int (1) [2] => int (7) [3] => Int (5) [4] => int (0) [5] => int (2) [6] => int (4) [7] => int (6)} [36] => array (8) {[0] => int (3) [1] => int (5) [2] => int (0) [3] => int (4) [4] => int (1) [5] => int (7) [6] => int (2) [7] => int (6)} [37] => array (8) {[0] => int (3) [1] => int (5) [2] => int (7) [3] => int (1) [4] => int (6) [5] => int (0) [6] => int (2) [7] => int (4)} [38] => array (8) {[0] => int (3) [1] => int (5) [2] => int (7) [3] => int (2) [4] => int (0) [5] => Int (6) [6] => int (4) [7] => int (1)} [39] => array (8) {[0] => int (3) [1] => int (6) [2] => int (0) [3] => int (7) [4] => int (4) [5] => int (1) [6] => int (5) [7] => int (2 )} [40] => array (8) {[0] => int (3) [1] => int (6) [2] => int (2) [3] => int (7) [4] => int (1) [5] => int (4) [6] => int (0) [7] => int (5)} [41] => array (8) {[0] => int (3) [1] => int (6) [2] => int (4) [3] => int (1) [4] => int (5) [5] => int (0) [6] => int (2) [7] => Int (7)} [42] => array (8) {[0] => int (3) [1] => int (6) [2] => int (4) [3] => int (2) [4] => int (0) [5] => int (5) [6] => int (7) [7] => int (1)} [43] => array (8) {[0] => int (3) [1] => int (7) [2] => int (0) [3] => int (2) [4] => int (5) [5] => int (1) [6] => int (6) [7] => int (4)} [44] => array (8) {[0] => int (3) [1] => int (7) [2] => int (0) [3] => int (4) [4] => int (6) [5] => int (1) [6] => int (5) [7] => int (2 )} [45] => array (8 ){[ 0] => int (3) [1] => int (7) [2] => int (4) [3] => int (2) [4] => int (0) [5] => int (6) [6] => int (1) [7] => int (5 )} [46] => array (8) {[0] => int (4) [1] => int (0) [2] => int (3) [3] => int (5) [4] => int (7) [5] => int (1) [6] => int (6) [7] => int (2)} [47] => array (8) {[0] => int (4) [1] => int (0) [2] => int (7) [3] => int (3) [4] => int (1) [5] => int (6) [6] => int (2) [7] => int (5)} [48] => array (8) {[0] => int (4) [1] => int (0) [2] => Int (7) [3] => int (5) [4] => int (2) [5] => int (6) [6] => int (1) [7] => int (3)} [49] => array (8) {[0] => int (4) [1] => int (1) [2] => int (3) [3] => int (5) [4] => int (7) [5] => int (2) [6] => int (0) [7] => int (6)} [50] => array (8) {[0] => int (4) [1] => int (1) [2] => int (3) [3] => int (6) [4] => int (2) [5] => int (7) [6] => int (5) [7] => int (0 )} [51] => array (8) {[0] => int (4) [1] => int (1) [2] => int (5) [3] => int (0) [4] => Int (6) [5] => int (3) [6] => int (7) [7] => int (2 )} [52] => array (8) {[0] => int (4) [1] => int (1) [2] => int (7) [3] => int (0) [4] => int (3) [5] => int (6) [6] => int (2) [7] => int (5)} [53] => array (8) {[0] => int (4) [1] => int (2) [2] => int (0) [3] => int (5) [4] => int (7) [5] => int (1) [6] => int (3) [7] => int (6)} [54] => array (8) {[0] => int (4) [1] => int (2) [2] => int (0) [3] => int (6) [4] => int (1) [5] => int (7) [6] => I Nt (5) [7] => int (3)} [55] => array (8) {[0] => int (4) [1] => int (2) [2] => int (7) [3] => int (3) [4] => int (6) [5] => int (0) [6] => int (5) [7] => int (1)} [56] => array (8) {[0] => int (4) [1] => int (6) [2] => int (0) [3] => int (2) [4] => int (7) [5] => int (5) [6] => int (3) [7] => int (1 )} [57] => array (8) {[0] => int (4) [1] => int (6) [2] => int (0) [3] => int (3) [4] => int (1) [5] => int (7) [6] => int (5) [7] => int (2)} [58] => Array (8) {[0] => int (4) [1] => int (6) [2] => int (1) [3] => int (3) [4] => int (7) [5] => int (0) [6] => int (2) [7] => int (5)} [59] => array (8) {[0] => int (4) [1] => int (6) [2] => int (1) [3] => int (5) [4] => int (2) [5] => int (0) [6] => int (3) [7] => int (7)} [60] => array (8) {[0] => int (4) [1] => int (6) [2] => int (1) [3] => int (5) [4] => int (2) [5] => int (0) [6] => int (7) [7] => int (3)} [61] => array (8) {[0] => int (4) [1] => Int (6) [2] => int (3) [3] => int (0) [4] => int (2) [5] => int (7) [6] => int (5) [7] => int (1)} [62] => array (8) {[0] => int (4) [1] => int (7) [2] => int (3) [3] => int (0) [4] => int (2) [5] => int (5) [6] => int (1) [7] => int (6 )} [63] => array (8) {[0] => int (4) [1] => int (7) [2] => int (3) [3] => int (0) [4] => int (6) [5] => int (1) [6] => int (5) [7] => int (2)} [64] => array (8) {[0] => int (5) [1] => int (0) [2] => int (4) [3] => Int (1) [4] => int (7) [5] => int (2) [6] => int (6) [7] => int (3)} [65] => array (8) {[0] => int (5) [1] => int (1) [2] => int (6) [3] => int (0) [4] => int (2) [5] => int (4) [6] => int (7) [7] => int (3)} [66] => array (8) {[0] => int (5) [1] => int (1) [2] => int (6) [3] => int (0) [4] => int (3) [5] => int (7) [6] => int (4) [7] => int (2)} [67] => array (8) {[0] => int (5) [1] => int (2) [2] => int (0) [3] => int (6) [4] => int (4) [5] => in T (7) [6] => int (1) [7] => int (3)} [68] => array (8) {[0] => int (5) [1] => int (2) [2] => int (0) [3] => int (7) [4] => int (3) [5] => int (1) [6] => int (6) [7] => int (4 )} [69] => array (8) {[0] => int (5) [1] => int (2) [2] => int (0) [3] => int (7) [4] => int (4) [5] => int (1) [6] => int (3) [7] => int (6)} [70] => array (8) {[0] => int (5) [1] => int (2) [2] => int (4) [3] => int (6) [4] => int (0) [5] => int (3) [6] => int (1) [7] => int (7)} [71] => array (8) {[0] => int (5) [1] => int (2) [2] => int (4) [3] => int (7) [4] => int (0) [5] => int (3) [6] => int (1) [7] => int (6)} [72] => array (8) {[0] => int (5) [1] => int (2) [2] => int (6) [3] => int (1) [4] => int (3) [5] => int (7) [6] => int (0) [7] => int (4)} [73] => array (8) {[0] => int (5) [1] => int (2) [2] => int (6) [3] => int (1) [4] => int (7) [5] => int (4) [6] => int (0) [7] => int (3)} [74] => array (8) {[0] => Int (5) [1] => int (2) [2] => int (6) [3] => int (3) [4] => int (0) [5] => int (7) [6] => int (1) [7] => int (4)} [75] => array (8) {[0] => int (5) [1] => int (3) [2] => int (0) [3] => int (4) [4] => int (7) [5] => int (1) [6] => int (6) [7] => int (2 )} [76] => array (8) {[0] => int (5) [1] => int (3) [2] => int (1) [3] => int (7) [4] => int (4) [5] => int (6) [6] => int (0) [7] => int (2)} [77] => array (8) {[0] => int (5) [1] => int (3) [2] => I Nt (6) [3] => int (0) [4] => int (2) [5] => int (4) [6] => int (1) [7] => int (7)} [78] => array (8) {[0] => int (5) [1] => int (3) [2] => int (6) [3] => int (0) [4] => int (7) [5] => int (1) [6] => int (4) [7] => int (2)} [79] => array (8) {[0] => int (5) [1] => int (7) [2] => int (1) [3] => int (3) [4] => int (0) [5] => int (6) [6] => int (4) [7] => int (2)} [80] => array (8) {[0] => int (6) [1] => int (0) [2] => int (2) [3] => int (7) [4] => int (5) [5] => int (3) [6] => int (1) [7] => int (4)} [81] => array (8) {[0] => int (6) [1] => int (1) [2] => int (3) [3] => int (0) [4] => int (7) [5] => int (4) [6] => int (2) [7] => int (5 )} [82] => array (8) {[0] => int (6) [1] => int (1) [2] => int (5) [3] => int (2) [4] => int (0) [5] => int (3) [6] => int (7) [7] => int (4)} [83] => array (8) {[0] => int (6) [1] => int (2) [2] => int (0) [3] => int (5) [4] => int (7) [5] => int (4) [6] => int (1) [7] => int (3)} [84] => array (8) {[0] => int (6) [1] => int (2) [2] => int (7) [3] => int (1) [4] => int (4) [5] => int (0) [6] => int (5) [7] => int (3)} [85] => array (8) {[0] => int (6) [1] => int (3) [2] => int (1) [3] => int (4) [4] => int (7) [5] => int (0) [6] => int (2) [7] => int (5)} [86] => array (8) {[0] => int (6) [1] => int (3) [2] => int (1) [3] => int (7) [4] => int (5) [5] => int (0) [6] => int (2) [7] => int (4 )} [87] => arra Y (8) {[0] => int (6) [1] => int (4) [2] => int (2) [3] => int (0) [4] => int (5) [5] => int (7) [6] => int (1) [7] => int (3)} [88] => array (8) {[0] => int (7) [1] => int (1) [2] => int (3) [3] => int (0) [4] => int (6) [5] => int (4) [6] => int (2) [7] => int (5)} [89] => array (8) {[0] => int (7) [1] => int (1) [2] => int (4) [3] => int (2) [4] => int (0) [5] => int (6) [6] => int (3) [7] => int (5)} [90] => array (8) {[0] => int (7) [1] => in T (2) [2] => int (0) [3] => int (5) [4] => int (1) [5] => int (4) [6] => int (6) [7] => int (3)} [91] => array (8) {[0] => int (7) [1] => int (3) [2] => int (0) [3] => int (2) [4] => int (5) [5] => int (1) [6] => int (6) [7] => int (4)} there is 92 ways! 0 seconds used.

It should also be noted that the final time-based calculation of variables with less rigorous precision php cannot be directly subtracted with severe errors. This is only a temporary demonstration. You must call the relevant functions for exact calculation.

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.