uva 524 回溯法
2015-08-10 21:26
357 查看
#include <bits/stdc++.h> using namespace std; int kase, n; const int prim[] = {1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; int ans[17]; bool vis[17]; bool is_primf(int x) { for (int i = 0; i < 13; i++) if (x == prim[i]) return 1; return 0; } void dfs(int cur, int now) { vis[cur] = 1; ans[now] = cur; if (now == n && is_primf(cur + ans[1])) { for (int i = 1; i <= n; i++) cout << ans[i] << (i == n ? "\n" : " "); } else for (int i = 1; i <= n; i++) if (!vis[i] && is_primf(i + cur)) dfs(i, now + 1), vis[i] = 0; } int main(int argc, char const *argv[]) { #ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin); freopen("2.txt", "w", stdout); #endif int first = 1; while (cin >> n) { if (!first) cout << endl; else first = 0;//末尾不用换行!!!!!!!!!!!! cout << "Case " << ++kase << ":" << endl; memset(vis, 0, sizeof(vis)); memset(ans, 0, sizeof(ans)); dfs(1, 1); } return 0; }
相关文章推荐
- 杭电ACM求数列的和
- c# 笔试题及参考答案大全
- HDU 4169 Wealthy Family 优先队列
- ***POJ 2152 - Fire(树形DP)
- git常用命令
- 数据同步平台——FTP
- YXBBS 2.3论坛系统后台拿shell 转自红黑联盟
- 运行hadoop自带的案例—wordcount
- OC之NSString
- 大数加法
- POJ 1952 BUY LOW, BUY LOWER
- Android JSON 解析库的使用 - Gson 和 fast-json
- 功夫。
- 浅谈OFBiz之权限设计
- JavaScricpt面向对象功能的理论基础
- 约瑟夫环问题 循环链表
- RSA加密 公钥 私钥
- [数据结构]Angle迷宫(纯裸BFS)
- 巧秒构造造就强大的adapter
- Android 编程下的 EditView 阻止软键盘自动弹出__图片资源文件后缀不能为.ico