hdu1016-Prime Ring Problem(回溯法)
来源:互联网 发布:霸道总裁 知乎 编辑:程序博客网 时间:2024/06/05 18:54
回溯法……没有感觉到和正常dfs的区别啊。不过还是值得记录的,给自己强调一点,字典序的时候按顺序dfs就好。
注意刘汝佳书上的输出由于每一段答案后多了个空格所以会PE。
#include <algorithm>#include <iostream>#include <sstream>#include <cstring>#include <cstdlib>#include <string>#include <vector>#include <cstdio>#include <stack>#include <cmath>#include <queue>#include <map>#include <set>using namespace std;#define N 100005const int INF=0x3f3f3f3f;int n,vis[25],a[25],isp[25];bool isprime(int x){ if (x%2==0) { return false; } for (int i=3; i<=sqrt(x); i+=2) { if (x%i==0) { return false; } } return true;}void dfs(int cur){ if (cur==n&&isp[a[0]+a[n-1]]) { for (int i=0; i<n-1; i++) { printf("%d ",a[i]); } printf("%d\n",a[n-1]); } else { for (int i=2; i<=n; i++) { if (!vis[i]&&isp[i+a[cur-1]]) { a[cur]=i; vis[i]=1; dfs(cur+1); vis[i]=0; } } }}int main() { int ti=0; while (cin>>n) { ti++; memset(vis, 0, sizeof(vis)); memset(isp, 0, sizeof(isp)); for (int i=2; i<=n*2; i++) { isp[i]=isprime(i); } printf("Case %d:\n",ti); a[0]=1; dfs(1); cout<<endl; } return 0;}
0 0
- hdu1016-Prime Ring Problem(回溯法)
- hdu1016 Prime Ring Problem(回溯dfs)
- HDU1016 Prime Ring Problem 【回溯】
- hdu1016(Prime Ring Problem)
- hdu1016 Prime Ring Problem()
- HDU1016 Prime Ring Problem
- hdu1016 Prime Ring Problem
- hdu1016-Prime Ring Problem
- HDU1016 Prime Ring Problem
- hdu1016 Prime Ring Problem
- hdu1016 Prime Ring Problem
- HDU1016-Prime Ring Problem
- HDU1016 Prime Ring Problem
- HDU1016 Prime Ring Problem
- hdu1016 Prime Ring Problem
- HDU1016 Prime Ring Problem
- [HDU1016]Prime Ring Problem
- hdu1016 Prime Ring Problem
- Neural Networks for Machine Learning by Geoffrey Hinton (3)
- UVA 317 Hexagon
- Android之UI
- 挑战程序设计竞赛里面的部分题目<用java写的>
- ios第三方库集成
- hdu1016-Prime Ring Problem(回溯法)
- PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自测5. Shuffling Machine (20) (简单模拟)【二星级】
- HDU 5333 Undirected Graph【LCT+BIT】
- Java集合学习--HashMap、LinkedHashMap、TreeMap、HashTable
- 敏捷开发之Scrum基础
- 时间和日期函数
- 类与类之间的关系
- 基于RestEasy开发环境搭建
- Linux下使用inotify实现对文件的监控