PHP implements graph-based depth-first traversal to output the full arrangement of 1, 2, 3... n, php3... n

Source: Internet
Author: User

PHP implements graph-based depth-first traversal to output the full arrangement of 1, 2, 3... n, php3... n

The example in this article describes how PHP implements a graph-based depth-first traversal to output the full arrangement of 1, 2, 3... n. We will share this with you for your reference. The details are as follows:

<? Php $ n = $ _ REQUEST ["n"]; if ($ n> 8) {echo "{$ n} is too large, affecting server performance"; return ;} define ("N", $ n); $ d = array (); $ v = array (); for ($ I = 0; $ I <= N; $ I ++) {$ d [$ I] = $ v [$ I] = 0;} function dfs ($ depth) {global $ d, $ v; if ($ depth> = N) {for ($ I = 0; $ I! = N; $ I ++) {echo $ d [$ I];} echo "<br>"; return;} for ($ I = 1; $ I <= N; $ I ++) {if ($ v [$ I] = 0) {$ v [$ I] = 1; $ d [$ depth] = $ I; dfs ($ depth + 1); $ v [$ I] = 0 ;}} dfs (0 );

Here, the get method is used to input the parameter n = 4. The output is as follows:

123412431324134214231432213421432314234124132431312431423214324134123421412341324213423143124321

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.