hdu1015: Given a series of values of available sequences, each character is mapped to an integer between 1-26, selected so as to satisfy the ordered number five ab & 2 + C . 3 -d . 4 + E . 5 = target.
1 #include<iostream> 2 #include<cstdio> 3 #include<string.h> 4 #include<algorithm> 5 #include<map> 6 using namespace std; 7 int target; 8 char s[15]; 9 int vis[28]; 10 map<char,int>mp; 11 char ans[10],ch[10]; 12 int num[10]; 13 int len; 14 bool fin(int *a) 15 { 16 return a[0]-a[1]*a[1]+a[2]*a[2]*a[2]-a[3]*a[3]*a[3]*a[3]+a[4]*a[4]*a[4]*a[4]*a[4]==target; 17 } 18 void init() 19 { 20 memset(vis,0,sizeof(vis)); 21 memset(ans,'\0',sizeof(ans));//空串字典序最小 22 memset(ch,'\0',sizeof(ch)); 23 memset(num,0,sizeof(num)); 24 char c='A'; 25 for(int i=1;i<=26;i++) 26 mp[c++]=i; //Construction mapping relationship 27 } 28 29 void DFS ( int CUR) // number of bits currently processed 30 { 31 is IF (CUR == . 5 ) 32 { 33 is IF (FIN (NUM) && strcmp (CH, ANS)> 0 ) 34 is strcpy (ANS, CH); 35 return ; 36 } 37 [ for ( int I = 0 ; I <len; I ++ ) 38 is { 39 IF (! VIS [MP [S [I]]]) 40 { 41 is VIS [MP [s [i]]] = . 1 ; 42 is CH [CUR] = s [i]; // store character s [i] for comparison with ANS 43 is NUM [CUR] MP = [s [i]] ; // converted to digital, to determine whether the end 44 is DFS (CUR + . 1 ); 45 VIS [MP [S [I]]] = 0 ; 46 is } 47 } 48 return ; 49 } 50 int main () 51 is { 52 is 53 is the while (Scanf ( " % S% D " , target &, & S) ==2&&!(target==0&&strcmp(s,"END")==0)) 54 { 55 init(); 56 // cout<<"target:"<<target<<" "<<s<<endl; 57 len=strlen(s); 58 sort(s,s+len); 59 dfs(0); 60 if(!strcmp(ans,""))cout<<"no solution"<<endl; 61 else cout<<ans<<endl; 62 } 63 }
hdu1016 n number of full-circle configuration arranged such that two adjacent numbers and the first number and a prime number is 1. Preprocessing the first prime number less than 40, then the deep search.
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n; int a[21]; int isp[40]; int vis[25]; bool isprime(int x) { if(x<2)return false; for(int i=2;i*i<=x;i++) { if(x%i==0) return false; } Return to true ; } void DFS ( int CUR) // Search Depth { int I; IF (CUR == n-&& ISP [(A [cur . 1 ] + A [ 0 ])]) // recursive termination conditions { for ( = I 0 ; I <N- . 1 ; I ++ ) the printf ( " % D " , A [I]); the printf ( " % D \ n- " , A [N- . 1 ]); } the else for (I = 2 ; I <= n-; I ++ ) { if(!vis[i]&&isp[i+a[cur-1]]) { a[cur]=i; vis[i]=1; dfs(cur+1); vis[i]=0; //状态回复 } } } int main() { for(int i=0;i<=40;i++)isp[i]=isprime(i); int t=1; while(scanf("%d",&n)!=EOF) { memset(vis,0,sizeof(vis)); printf("Case %d:\n",t); a[0]=1; dfs(1); printf("\n"); t++; } }