hdu 1016 Prime Ring Problem(DFS)
2015-10-27 20:11
387 查看
原题链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1016
思路:
DFS……
代码如下:
http://acm.hdu.edu.cn/showproblem.php?pid=1016
思路:
DFS……
代码如下:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAXN=100; const int N=20; int ans ,n,num; bool vis ; const int prime[]={2,3,5,7,11,13,17,19,23,29,31,37};//素数,因为只有1-19 bool isPrime(int x) { for(int i=0;i<12;i++) if(x==prime[i]) return true; return false; } void DFS(int s) { if(s==n&&isPrime(ans[s-1]+1))//边界 勿忘判断第一个数和最后一个数的和 { for(int i=0;i<s-1;i++) printf("%d ",ans[i]); printf("%d\n",ans[s-1]); return; } for(int i=2;i<=n;i++) { if(!vis[i]&&isPrime(ans[s-1]+i)) { ans[s]=i; vis[i]=true; DFS(s+1); vis[i]=false; } } } int main() { int kase=1; while(scanf("%d",&n)!=EOF) { memset(vis,0,sizeof(vis)); ans[0]=1; vis[1]=true; printf("Case %d:\n",kase); kase++; DFS(1); printf("\n"); } return 0; }
相关文章推荐
- phpredis中文手册——《redis中文手册》 php版
- android ListView加载不同布局
- android ListView加载不同布局
- 信息安全系统设计基础第六周学习总结
- 使用mongoosejs链接Mongodb
- AsyncTask下载图片
- 从西数购闪迪见紫光背影 看国厂曲线救国之路
- Android设计模式系列--工厂方法模式
- RESTful Web API中的Http协议语义
- python基础教程第一章知识点记录
- hdu 4600 Harvest Moon(模拟)
- 晶体结构
- 【Android网络编程】获取网络图片,具有缓存功能
- hdu 1430 魔板
- 120. Triangle(Array; DP, WPS)
- Android设计模式系列-单例模式
- 一趟聚类
- #define详解
- COGS1532. [IOI2001]移动电话
- 如何判断一个已经写好的MFC程序是单文档还是多文档?