hdu-1016 Prime Ring Problem 搜索题
Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
Note: the number of first circle should always be 1.
Note: the number of first circle should always be 1.

Input
n (0 < n < 20).
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input
6 8
Sample Output
简单的搜索题目,不过我还是好长时间没通过。。。
code:
#include<stdio.h>
#include<string.h>
int num[40];
int mark[120];
int prime[40];
void dfs(int root,int n,int m){
int i,j;
if(m>n&&!prime[num[n]+num[1]]){ //当搜索完后且第一个数和最后一个数也符合条件
for(j=1;j<n;j++) //的时候输出
printf("%d ",num[j]);
printf("%d\n",num[n]); //前边的数跟后边的数分开输出
return;
}
for(i=1;i<=n;i++){
if(!prime[i+root]&&!mark[i]){ //对数进行判断,如果每相邻两个数之和都为素数且未被标记
num[m]=i; //则满足条件
mark[i]=1; //对处理过的数字进行标记
dfs(i,n,m+1); //返回下一个要搜索的数字
mark[i]=0;
}
}
}
int main(){
int k,n,m,t,i,j;
int cas=1;
memset(prime,0,sizeof(prime));
prime[0]=prime[1]=1;
for(i=2;i*i<40;i++){ //打表,不是素数的标记为1
if(!prime[i])
for(j=i*i;j<40;j+=i)
prime[j]=1;
}
while(~scanf("%d",&n)){
memset(mark,0,sizeof(mark));
printf("Case %d:\n",cas++);
num[1]=1;
mark[1]=1;//1不用判断,直接标记
dfs(1,n,2);
printf("\n");
}
return 0;
}
南阳oj上边有一道几乎相同的题目:
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=488
大体思路还是相同的
代码:
#include<stdio.h>
#include<string.h>
int mark[50];
int num[50];
int prime[50];
void dfs(int root,int n,int m){
int i,j;
if(m>n&&!prime[num[1]+num[n]]){
for(j=1;j<=n;j++)
printf("%d ",num[j]);
printf("\n");
// printf("%d\n",num[n]);
}
// printf("No Answer\n");
for(i=1;i<=n;i++){
if(!mark[i]&&!prime[i+root])
{
num[m]=i;
mark[i]=1;
dfs(i,n,m+1);
mark[i]=0;
}
}
return;
}
int main(){
int i,j,k,t;
int n,m;
int Case=1;
memset(prime,0,sizeof(prime));
prime[0]=prime[1]=1;
for(i=2;i<50;i++){
if(!prime[i])
for(j=i*i;j<50;j+=i)
prime[j]=1;
}
//for(i=0;i<50;i++){
// if(!prime[i])
// printf("%d ",i);
//}
while(~scanf("%d",&n),n!=0){
memset(mark,0,sizeof(mark));
memset(num,0,sizeof(num));
num[1]=1;
mark[1]=1;
printf("Case %d:\n",Case++);
if(n==1)
printf("%d\n",1);//这部分比较容易忽略,
else if(n&1)
printf("No Answer\n");
else
dfs(1,n,2);
// printf("\n");
}
return 0;
}
分类:
基础算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理