Prime Ring Problem(UVA 524)(回溯法)
来源:互联网 发布:同一网络怎么共享文件 编辑:程序博客网 时间:2024/06/04 00:00
http://vjudge.net/problem/UVA-524
#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <set>#include <iterator>#include <iostream>using namespace std;int isp[50];int n, a[20], vis[20];int is_prime(int num){ for (int i = 2; i * i <=num; i++) { if (num % i == 0) return 0; } return 1;}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", a[n - 1]); printf ("\n"); } 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(){ #ifndef ONLINE_JUDGE freopen ("in.txt", "r", stdin); #endif // ONLINE_JUDGE int kase = 1; while (scanf ("%d", &n) != EOF) { if (kase != 1) {printf ("\n");} printf ("Case %d:\n", kase++); for (int i = 2; i <= n * 2; i++) isp[i] = is_prime (i); memset (a, 0, sizeof(a)); memset (vis, 0, sizeof(vis)); a[0] = 1; vis[1] = 1; dfs (1); } return 0;}
0 0
- UVa 524 - Prime Ring Problem(回溯)
- UVa 524:Prime Ring Problem(回溯)
- UVa 524 Prime Ring Problem (回溯)
- UVa 524 - Prime Ring Problem(回溯)
- Prime Ring Problem(UVA 524)(回溯法)
- UVA - 524 Prime Ring Problem (经典回溯问题)
- UVa 524 Prime Ring Problem(DFS , 回溯)
- hdu1016-Prime Ring Problem(回溯法)
- Prime Ring Problem(经典回溯法)
- UVA 524 素数环Prime Ring Problem (回溯法)
- hdu 1016 Prime Ring Problem(回溯)
- hdu1016 Prime Ring Problem(回溯dfs)
- hdu--1016 Prime Ring Problem(回溯法求解)
- Prime Ring Problem (HDU 1016)—— 回溯法
- prime ring problem回溯法
- uva - 524 - Prime Ring Probl(素数环、回溯)
- Prime Ring Problem(回溯)
- 素数环(Prime Ring Problem,UVa 524)
- 自定义java.lang.Object类会怎样
- c++链表
- windows下命令行新建删除用户
- NIM游戏POJ 1067 神奇的黄金分割
- 文件基本操作
- Prime Ring Problem(UVA 524)(回溯法)
- codeforces 721C. Journey(dp+拓扑)
- PHP开发环境
- [前端]input框 type="radio"显示不出
- 2016.9.18爱奇艺笔试编程题
- 【codeforces 721D】【贪心 堆】D. Maxim and Array 【给出n个数,k次机会,每次使得任意一个数字减少或者加上x,使得最后的乘积最小,最后的n个数大小】
- 八数码的八境界(待更新...)
- Codeforces Round #374 (Div. 2) C. Journey
- CentOS 6.3用yum安装中文输入法