HDU 1016 DFS
来源:互联网 发布:unity3d大作动画 编辑:程序博客网 时间:2024/06/14 03:30
原网址点击打开链接
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49383 Accepted Submission(s): 21783
Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
Note: the number of first circle should always be 1.
Note: the number of first circle should always be 1.
Input
n (0 < n < 20).
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input
68
Sample Output
Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<iostream>using namespace std;int Prime[45]={0};int a[20],vis[20];//vis[i]为标记节点是否访问,i为节点代号,数组内容为0或1,1代表未被访问//a[i]以数组形式储存符合条件的数字int m;int zz=1;int DFS(int step){ if(step==m+1&&!Prime[a[m]+a[1]]) { for(int i=1;i<m;i++) { printf("%d ",a[i]); } cout<<a[m]<<endl;//输出满足题目所需 return 0; } for(int i=2;i<=m;i++) { if(!vis[i]&&!Prime[i+a[step-1]])//i没被使用过,同时i与上一个数的和为素数 { vis[i]=1; a[step]=i; DFS(step+1); vis[i]=0;//回溯作用是此数组多次使用 } }}int main(){ for(int i=1; i<=40; i++) { for(int j=2; j*j<=i; j++) { if(i%j==0) { Prime[i]++; break; } } } a[1]=1; //int m; int k=1; while(scanf("%d",&m)!=EOF) { printf("Case %d:\n",k++); DFS(2); cout<<endl; } return 0;}最近一直在回顾之前所学,发现自己的知识储备已经所剩无几,最近做题一直没有什么思路,这道题看了半天也没啥思路,幸好参考了前辈的代码,DFS和BFS的运用一直是弱项,这道题蛮经典的,增加了我对DFS的理解,感谢大神博客
0 0
- hdu 1016 (dfs)
- hdu 1016dfs
- hdu 1016 (DFS)
- hdu 1016 dfs
- hdu 1016 DFS
- HDU 1016 DFS
- HDU-1016 dfs
- HDU 1016 DFS
- hdu 1016 DFS基础
- HDU 1016(dfs)
- dfs hdu 1016
- HDU 1016 dfs+回溯
- hdu 1016(dfs+dp)
- hdu 1016 (dfs)
- hdu 1016 dfs+回溯
- HDU 1016 DFS
- hdu--1016 素素环(dfs)
- hdu 1016 dfs(素数环)
- Android中Binder的理解
- Virtualenv在执行虚拟环境建立后没有bin目录的处理
- H5表单和一些新属性
- Android studio 连接设备
- Mysql 其他函数
- HDU 1016 DFS
- 深度学习基础系列 (一) 之基础概念
- 回溯法之迷宫 栈的引用
- 上下移动~上下拖动实现思路流程
- JAVA基础学习---- 继承与合成(构造器)
- VC中出现“烫烫烫”的原因分析
- Python正则表达式
- js高考倒计时
- 关于算法不断优化的乐趣