[hdu]1016 DFS entry question

Source: Internet
Author: User

The title means to find out that all adjacent numbers are added as a sequence of primes between all sequences from 1 to N. Ps: The title is ring, so the head and tail also to calculate OH ~

Typical DFS, and then prune.

The interesting thing is to use Java to run back to the edge of the tle, the first commit on the TLE (server load Problem bar), the same second commit on the AC, the side also reflects the recursion to stack overhead impact efficiency is also serious. Okay, on the code!!

Topic Portal:

hdu_1016

ImportJava.util.Scanner; Public classMain { Public Static Final int[] prime = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0 , 1, 0, 0,            0, 1 };  Public Static Boolean[] visited;  Public Static int[] nums;  Public Static intN;  Public Static voidDfsintDeepth,intCurrent ) {        if(Deepth = =N) {            if(prime[current+1]==1){             for(inti=0;i<n;i++) {System.out.print (nums[i]); if(i+1!=N) {System.out.print (" " );            }} System.out.println (); }        }        Else {             for(inti=2;i<=n;i++){                if(!visited[i] && prime[current+i]==1) {Visited[i]=true; Nums[deepth]=i; DFS (deepth+1, i); Visited[i]=false; }            }        }            }     Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); intC=1;  while(Sc.hasnext ()) {n=Sc.nextint (); Nums=New int[N+1 ]; Visited=New Boolean[N+1 ]; nums[0]=1; System.out.println ("Case" +c+ ":"); DFS (The); C++;        System.out.println (); }    }}

[hdu]1016 DFS entry question

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.