Simple DFS, from 1 ~ N is composed of digits. Two Adjacent numbers are required to form a prime number.
The input is relatively small. You can screen out the prime number or directly judge it.
CLR(prime,0); prime[1]=1; for(int i=2;i<101;i++) for(int j=2;i*j<101;j++) prime[i*j]=1;
Then, run the DFS command again.
#include<cstdio>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<stack>#include<iostream>#include<list>#include<set>#include<vector>#include<cmath>#define INF 0x7fffffff#define eps 1e-8#define LL long long#define PI 3.141592654#define CLR(a,b) memset(a,b,sizeof(a))#define FOR(i,a,n) for(int i= a;i< n ;i++)#define debug puts("==fuck==")#define acfun std::ios::sync_with_stdio(false)#define SIZE 1000+10using namespace std;int n;int a[21];bool vis[21];bool prime[101];void dfs(int m){ if(m>n) { FOR(i,0,n-1) printf("%d ",a[i]); printf("%d\n",a[n-1]); } else if(m==n) { if(!prime[a[m-1]+1]) dfs(m+1); } else { FOR(k,2,n+1) { if(!prime[a[m-1]+k]&&!vis[k]) { vis[k]=1; a[m]=k; dfs(m+1); vis[k]=0; } } }}int main(){ CLR(prime,0); prime[1]=1; for(int i=2;i<101;i++) for(int j=2;i*j<101;j++) prime[i*j]=1; int cot=1; while(~scanf("%d",&n)) { printf("Case %d:\n",cot++); CLR(vis,0); vis[1]=1; a[0]=1; dfs(1); printf("\n"); }}
HDU 1016 prime ring Problem