UVa 524 - Prime Ring Problem(回溯)
来源:互联网 发布:淘宝首页psd模板教程 编辑:程序博客网 时间:2024/06/07 06:45
回溯法的简单应用。
#include<iostream>#include<cstdio>#include<string>#include<algorithm>#define MAXN 50using namespace std;int n, A[MAXN] = {1}, ispe[MAXN], vis[MAXN];void dfs(int cur) { if(cur == n&& ispe[A[0] + A[n - 1]]) { for(int i = 0; i < n; i++) { i ? printf(" %d", A[i]) : printf("%d", A[i]); } printf("\n"); } else for(int i = 2; i <= n; i++) { if(!vis[i]&& ispe[i + A[cur - 1]]) { A[cur] = i; vis[i] = 1; dfs(cur + 1); vis[i] = 0; } }}int main() { for(int i = 2; i <= 50; i++) ispe[i] = 1; for(int i = 2; i <= 50; i++) for(int j = i + i; j + i <= 50; j += i) ispe[j] = 0; int kase = 0; while(cin >> n) { if(kase++) printf("\n"); printf("Case %d:\n", kase); 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 , 回溯)
- UVA 524 素数环Prime Ring Problem (回溯法)
- Prime Ring Problem(回溯)
- UVA 524 - Prime Ring Problem
- uva 524 - Prime Ring Problem
- uva 524 Prime Ring Problem
- UVa:524 Prime Ring Problem
- uva 524 - Prime Ring Problem
- UVa 524 - Prime Ring Problem
- UVA - 524 Prime Ring Problem
- UVa-524 - Prime Ring Problem
- UVA - 524 Prime Ring Problem
- isspace()
- Matlab Intro - Image Structure
- width:auto和width:100%的区别
- python 抓取lofterart动态页面
- C++中的locale设置
- UVa 524 - Prime Ring Problem(回溯)
- C#中listview的image添加问题和item双击事件
- Python设计模式——责任链模式
- GreenDao分析概括
- C++游戏之GDI的初步认识与使用(二)
- 认识处理器-寄存器使用(待改)
- Java网络编程
- mvel2.0语法指南
- 财务部发动置换万亿当地债 改进银行流动性