PHP-based ant crawler path algorithm

Source: Internet
Author: User
: This article mainly introduces the PHP version of the ant crawler path algorithm. if you are interested in the PHP Tutorial, please refer to it.
 $ I) {// return $ direararr;} if (0 = $ direararr [$ I]) {// add the current bit 1 $ direararr [$ I] = 1; return $ direararr;} $ direararr [$ I] = 0; return add2 ($ direararr, $ count, $ I-1); // carry} $ positionArr = array (// Location: 3, 7, 11, 17, 23); function path ($ positionArr) {// Generate the test path $ pathCalculate = array (); $ count = count ($ positionArr); $ directionArr = array_fill (0, $ count, 0 ); // orientation $ end = str_repeat ('1', $ coun T); while (true) {$ path = implode ('', $ directionArr); $ pathArray = array_combine ($ positionArr, $ direararr); $ total = calculate ($ positionArr, $ direararr); $ pathCalculate ['P '. $ path] = $ total; if ($ end = $ path) {// break for traversal completion;} $ direararr = add2 ($ direararr, $ count, $ count-1);} return $ pathCalculate;} function calculate ($ positionArr, $ directionArr) {$ total = 0; // $ length = 27 in total; // Wood rod length while ($ PositionArr) {$ total ++; // step increase time $ nextArr = array (); // the next position foreach ($ positionArr as $ key => $ value) {if (0 = $ direararr [$ key]) {$ next = $ value-1; // take a step toward 0} else {$ next = $ value + 1; // take a step toward 1} if (0 = $ next) {// walk out of continue in the 0 direction;} if ($ length = $ next) {// exit continue in the 1 direction;} $ nextArr [$ key] = $ next;} $ positionArr = $ nextArr; foreach ($ nextArr as $ key => $ value) {$ findArr = array_keys ($ positionArr, $ Value); if (count ($ findArr) <2) {// no overlapped position continue;} foreach ($ findArr as $ findIndex) {$ direararr [$ findIndex] = $ direararr [$ findIndex]? 0: 1; // unset ($ positionArr [$ findIndex]); }}return $ total ;}$ pathCalculate = path ($ positionArr); echo'
calculate-';print_r($pathCalculate);echo 'sort-';asort($pathCalculate);print_r($pathCalculate);

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the PHP version of the ant crawler path algorithm, including the content, hope to be helpful to friends interested in PHP tutorials.

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.