PHP-implemented Ant crawler path algorithm code

Source: Internet
Author: User
This article mainly introduces the ant climbing path algorithm code implemented by PHP, and analyzes the principle and implementation method of the ant climbing path algorithm in the form of a complete example, it involves php numeric calculation and array operation related skills. if you need it, you can refer to the example in this article to describe the ant crawler path algorithm code implemented by PHP. We will share this with you for your reference. The details are as follows:

<? Php/*** has a 27 cm sub-pole, each of which has an ant in the five positions 3 cm, 7 cm, 11 cm, 17 cm. * The wooden pole is very fine and cannot pass through an ant at the same time. At the beginning, the head of the Ant Financial system is either left or right. they only move forward or turn their heads, * but do not move back. When any two ants meet, they both turn their heads to the opposite direction. It is assumed that the Ant Financial can walk 1 cm away every second. * Write a program to find the minimum time and maximum time for all ants to leave the wooden pole. */Function add2 ($ direararr, $ count, $ I) {if (0> $ I) {// return $ direararr beyond the calculation range ;} if (0 = $ direararr [$ I]) {// add 1 $ direararr [$ I] = 1; return $ direararr ;} $ directionArr [$ 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); $ direc TionArr = array_fill (0, $ count, 0); // orientation $ end = str_repeat ('1', $ count); 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 calcu Late ($ positionArr, $ directionArr) {$ total = 0; // $ length = 27 in total; // The length of the wood rod 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; // Set $ positionArr to a temporary query array 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]); // prevent repeated search calculation} $ positionArr = $ nextArr; // Set $ positionArr to the next result array} return $ total;} $ pathCalculate = path ($ positionArr); echo'
calculate-';print_r($pathCalculate);echo 'sort-';asort($pathCalculate);print_r($pathCalculate);

I hope this article will help you with PHP programming.

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.