This topic is the primary topic of DFS, but I am almost ignorant of Dfs, so it is very difficult to write this question, but write this after the other topic is very easy, the problem is to find prime ring, a very basic deep search
#include <iostream> #include <string.h> using namespace std;
int prime[41]={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};
int n;
BOOL VIS[25];
int a[25];
void Dfs (int pos)//This is where the location {//cout<<pos<<endl;
if (Pos==n+1&&prime[a[n]+a[1]])//This must be n+1, because at n time, the nth bit has not processed {for (int i=1;i<pos-1;i++) {
cout<<a[i]<< "";
} cout<<a[pos-1]<<endl;
cout<<endl;
Return
} for (int i=2;i<=n;i++) {if (!vis[i]&&prime[i+a[pos-1]]) {
A[pos]=i;
Vis[i]=true;
DFS (POS+1);
Vis[i]=false;
}}} int main () {int cases=1;
while (cin>>n) {cout<< ' case ' <<cases++<< ': ' <<endl;
memset (vis,0,sizeof (VIS));
A[1]=1;
Vis[1]=true;
DFS (2);cout<<endl;
} return 0;
}