hdu 1016 Prime Ring Problem
来源:互联网 发布:js获取表单radio 编辑:程序博客网 时间:2024/06/06 11:49
题意:相邻两节点的值是素数,且是环形数据。
方法:用depth first search 方法,就可以得到答案了。
1)C语言
#include<iostream>using namespace std;/*思路:给环顺序编号,例如:n=6,环看成链,数组下标值为1,2,3,4,5,6;单词命名提示:visit数组存放-1,1两个值,分别表示未访问,已访问arr--array数组的意思,保存当前结点的值*/int visit[21],arr[21];int n;int main(){ void depthFirstSearch(int num); int count = 0; while(cin>>n){ fill(visit+1,visit+n+1,-1);// 整体数据右移一位 fill(arr+1,arr+n+1,1);// 整体数据右移一位 cout<<"Case "<<++count<<":"<<endl;// 数据提示行 visit[1] = 1; depthFirstSearch(1);// 默认从第一个数据开始搜索 cout<<endl;// 输出空白行 } return 0;}/*传入一个参数,表示搜索num编号的圆圈,因为圆圈没有相对应的数组,所以传参*/void depthFirstSearch(int num){ bool isPrime(int n); /* 要写鸿沟 */ if(num==n&&isPrime(1+arr[num])){ /* 深搜到次,可以知道,答案了。 链表每一次对一个圆圈给值,如果满足就继续深搜, 不满足就回退,除非没有结果。 */ for(int i=1;i<n;i++){ cout<<arr[i]<<" "; } cout<<arr[n]<<endl; return; } /* 这是一个完全图的遍历 */ for(int i=1;i<=n;i++){ /* 判断哪一个值没有被访问 */ if(i!=num&&visit[i]!=1){ /* 父节点的值与孩子接点的值i之和,如果是素数, 那么就以该孩子进行搜索,依次类推。 */ if(isPrime(i+arr[num])){ /* 1)标记当前圆圈已被遍历 2)搜索当前结点 3)回退,要修改成未被遍历 */ visit[i] = 1; arr[num+1] = i; depthFirstSearch(num+1); visit[i] = -1; arr[num+1] = 1; } } }}bool isPrime(int n){ if(n==2){ return true; } for(int i=2;i*i<=n;i++){ if(n%i==0){ return false; } } return true;}
java代码
import java.util.Scanner;/* * 思路: * 把1-n之间的数放入环中,所有的数都必须放入,且没有重复的数字 * 这时使用depthFirstSearch法逐个放入,合格的数就放入,不合格的就回退。 */public class Main { static int n; /* * visit用来判断第i个圆圈是否被遍历 * --1表示已被遍历,-1表示未被遍历, * ring用来存储遍历序列 */ static int visit[] = new int[21]; static int ring[] = new int[21]; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = 1; while (sc.hasNext()) { // 输入数据 n = sc.nextInt(); // 初始化数据 for (int i = 1; i <= n; i++) { visit[i] = -1; ring[i] = 1; } // 处理数据,并输出结果 System.out.println("Case " + (count++) + ":"); visit[1] = 1;// 自己已被搜索 depthFirstSearch(1); System.out.println(); } } /* * num是用来告诉DFS搜索指定Num下标的圆圈值 */ private static void depthFirstSearch(int num) { /* * 鸿沟 * 答案 */ if (num == n && isPrime(ring[n] + ring[1])) { for (int i = 1; i < n; i++) { System.out.print(ring[i] + " "); } System.out.println(ring[n]); return; } /* * 这里应该用完全搜索 * 自己本身是不搜索 */ for (int i = 1; i <= n; i++) { /* * 1)标记已被遍历 * 2)搜索下一个圆圈 * 3)擦掉标记 */ if (i != num && visit[i] != 1) { if (isPrime(i + ring[num])) { visit[i] = 1; ring[num + 1] = i; depthFirstSearch(num + 1); visit[i] = -1; ring[num + 1] = 1; } } } } private static boolean isPrime(int n) { if (n == 2) { return true; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; }}
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 33927 Accepted Submission(s): 15019
Problem Description
A ring is compose of n circles as shown in diagram.
一个环由n个圆圈组成,如图所示。
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.
注意:圆圈的第一个数是1。
把自然数分别放到下面的圆圈中,每两个相邻的圆圈可以组合成一个素数。
Note: the number of first circle should always be 1.
注意:圆圈的第一个数是1。
Input
n (0 < n < 20).
输入的n小于20大于0。
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.
每一行代表一系列的圆圈号,分别从1到n,这里的输出是先顺序输出,然后逆序输出,其实用depth first search 就满足他的输出格式了。
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
Source
Asia 1996, Shanghai (Mainland China)
0 0
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU-1016 prime ring problem
- hdu 1016Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU-1016 Prime Ring Problem
- HDU-1016-Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- Hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- Hdu 1016 - Prime Ring Problem
- HDU 1016 Prime Ring Problem
- Public Sale HDOJ
- Segmentation Fault in Linux 原因与避免
- C++_表达式x&(x-1)
- NSString基本用法
- leetcode 61: Rotate List
- hdu 1016 Prime Ring Problem
- iOS 开发中界面层与网络相关的部分通用功能的的设计
- 极客五控
- 黑马程序员-集合-Set和Map和Collections
- java单例模式
- RSA算法原理(一)
- swift学习之...和..<
- RSA算法原理(二)
- 堆排序