Depth first search
Depth first search (DFS) is an algorithm for traversing or searching trees or graphs. The nodes of the tree are traversed along the depth of the tree, and the branches of the tree are searched as deep as possible. When all the edges of node v have been explored, the search will go back to the starting node of the edge where node v is found. This process continues until all nodes reachable from the source node have been found. If there are still undiscovered nodes, select one of them as the source node and repeat the above process. The whole process repeats until all nodes are accessed.
It's interesting that John Hopcroft and Robert Tayan won the top prize in the computer field in 1986: Turing Award for their invention of "depth first search algorithm".
Eight queens question
Classic introduction to dfs
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=12; int map[maxn][maxn]; int ans,n; int ans_arr[maxn]; void dfs(int ithqueue); int main() { for(n=1;n<maxn;++n) //n for how many queens { ans=0; for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j)map[i][j]=0; } dfs(1); //Start with the first one ans_arr[n]=ans; } int q; while(~scanf("%d",&q),q){ printf("%d\n",ans_arr[q]); } return 0; } void dfs(int ithqueue){ //ithqueue represents the current number if(ithqueue==n+1){ //If you put it in n+1, it means it's done. ans++; return ; } for(int col=1;col<=n;++col) //It's obvious that the first queen represents the line, col starts scanning from 1 to n, which means the current queen is placed in the column { if(map[ithqueue][col]==0) //Equal to 0, means it can be put { for(int i=1;i<=n;++i) //Mark if the slash of the row and column is 0 { for(int j=1;j<=n;++j) { if(( i==ithqueue || col==j || abs(i-ithqueue)==abs(col-j) ) && map[i][j]==0) { map[i][j]=ithqueue; } } } dfs(ithqueue+1); //Put down a queen for(int i=1;i<=n;++i) //To flash back { for(int j=1;j<=n;++j) { if(map[i][j]==ithqueue) map[i][j]=0; } } } } }
Prime ring problem
Full Permutation 1 through deep search
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<queue> using namespace std; //Queue < int > A; / / I originally wanted to open the queue to save the answer. I think it will time out int arr[21]; //Used to mark whether the number has been changed int cir[21]; //Save answers int num,h,flag,T=1; void dfs(int n); int isPrime(int n); int main() { while(~scanf("%d",&num)) { for(int i=0;i<20;i++) arr[i]=1; flag=1; cir[1]=1; //Place 1 in the ring first arr[1]=0; //1 has been used dfs(2); //Put the second number printf("\n"); } return 0; } void dfs(int n){ if(flag) { printf("Case %d:\n",T); flag=0; T++; } if(n==num+1) { //Full, exit if(isPrime(1+cir[num])) { for(int i=1;i<num;++i) { printf("%d ",cir[i]); } printf("%d\n",cir[num]); } return ; } int i; for(i=2;i<=num;i++) //Scan over and over for the next number to place. { if(arr[i]==1 && isPrime(i+cir[n-1]) )//If the number is not let go, and the sum of the number and the number put in before is equal to the prime number, the number is put in. { arr[i]=0; //This number has been used h=i; cir[n]=i; //Put the number i in the ring dfs(n+1); //Start placing next number arr[i]=1; //To flash back } } if(i==num+1) return ; //I can't put the numbers in the back. It's over. } int isPrime(int n) { int i,k; k=sqrt(n); for(i=2;i<=k;i++) { if(n%i==0) break; } if(i<=k) return 0; else return 1; }
Netease: the minimum number of times
Full Permutation through deep search 2
#include<iostream> #include<algorithm> #include<cstring> using namespace std; int num[5]; int vis[5]; int stc[3]; int top; int maxn=1<<30; int ans; int gcd(int a,int b) { return b?gcd(b,a%b):a; } int lcm(int a,int b,int c) { int lcm1=a*b/gcd(a,b); int lcm2=lcm1*c/gcd(lcm1,c); return lcm2; } int dfs(int ith) { if(ith==4) { //cout<<stc[0]<<" "<<stc[1]<<" "<<stc[2]<<endl; //cout<<lcm(stc[0],stc[1],stc[2])<<endl; if(lcm(stc[0],stc[1],stc[2])<ans) { ans=lcm(stc[0],stc[1],stc[2]); } return 0; } for(int i=0;i<5;++i) { if(!vis[i]) { vis[i]=1; stc[top++]=num[i]; dfs(ith+1); vis[i]=0; top--; } } return 0; } int main() { while(cin>>num[0]>>num[1]>>num[2]>>num[3]>>num[4]) { memset(vis,0,sizeof(vis)); top=0; ans=maxn; dfs(1); cout<<ans<<endl; } return 0; }