DFS second question

Source: Internet
Author: User

Yesterday learned DFS, today, I did a problem, from 6:30 to 8:30, in the submission I thought not, after all, in addition to A+b, I did not have one-time ac over a problem, in the test maximum data, black box ran 34 seconds did not stop, I thought I would time out, but did not, good happy.

#include <iostream>#include<cstdio>#include<memory.h>using namespacestd;intN;inttemp[ -],cir[ -];intIs_prime (intX//determine if it is a prime number{     for(intI=2; i*i<=x;i++)        if(x%i==0)return 0; return 1;}voidDfsintAintN//deep Search, find sequence{    intA1; if(N==n&&is_prime (A +1)){//Stop search Criteria         for(intI=1; i<n;i++) cout<<cir[i]<<' '; cout<<cir[N]<<Endl; return; }     for(intI=2; i<=n;i++) {A1=i; if(temp[a1]==1)Continue; if(!temp[a1]&&a1>=1&&a1<=n&&is_prime (a1+a)) {Cir[n+1]=A1; TEMP[A1]=1; DFS (A1,n+1); TEMP[A1]=0; }    }}intMain () {intcount1=1;  while(SCANF ("%d", &n) = =1&&N) {cout<<" Case"<<count1++<<":"<<Endl; memset (temp,0,sizeof(temp)); memset (Cir,0,sizeof(CIR)); temp[1]=1; cir[1]=1; DFS (1,1); cout<<Endl; }}

Online search, found that their code can also be optimized, can be the prime number of tables, every time to check the table, no cycle, should be able to save a lot of time.

DFS second 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.