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