HDOJ 1016 Prime Ring Problem素数环【深搜】
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.
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.
Sample Input
6
8
Sample Output
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4
Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int Case = 0;
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int a[] = new int[n];
//初始数组1-n
int color[] = new int[n];
//判断数字是否已经存在
int prant[] = new int[n];
//输出数据排序
int count =0;//计数器
for(int i=0;i<n;i++){
a[i]=i+1;
color[i] = -1;
}//初始化数据
Case++;
System.out.println("Case "+(Case)+":");
dfs(a,color,prant,count,0);
System.out.println();
}
}
private static void dfs(int[] a, int[] color, int[] prant, int count,int m) {
//System.out.println(count);
count++;//计数器加1
if(count == a.length&&p(prant[0],a[m])){
//注意第一个数和最后一个数相加的和也必须为素数
prant[count-1]=a[m];
for(int i=0;i<a.length-1;i++){
System.out.print(prant[i]+" ");
}
System.out.println(prant[a.length-1]);
//return ;
}
for(int i=0;i<a.length;i++){
color[m] =1;
if(p(a[m],a[i])&&color[i]==-1){
color[i]=1;
prant[count-1]=a[m];
dfs(a,color,prant,count,i);
color[i]=-1;
}
}
}
//判断是不是素数
private static boolean p(int i, int j) {
int sum = i+j;
for(int a=2;a*a<=sum;a++){
if(sum%a==0){
return false;
}
}
return true;
}
}
C语言:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int n;
int df[21];
int t=1;
int m[21];
int mi;
bool pn(int x,int y){//判断素数
for(int i=2;i*i<=x+y;i++){
if((x+y)%i==0){
return false;
}
}
return true;
}
void dfs(int x){
if(mi==n&&pn(m[1],m[n])){
for(int i=1;i<n;i++){
printf("%d ",m[i]);
}
printf("%d\n",m[n]);
return;
}
for(int i=2;i<=n;i++){
if(df[i]==0&&pn(x,i)){
df[x]=1;
mi++;//当前小球数
m[mi]=i;
dfs(i);
df[x]=0;
mi--;//必须减一
}
}
}
int main()
{
while(~scanf("%d",&n)){
printf("Case %d:\n",t);
t++;
memset(df,0,sizeof(df));
mi=1;
m[mi]=1;
dfs(1);
printf("\n");
}
return 0;
}
HDOJ 1016 Prime Ring Problem素数环【深搜】的更多相关文章
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 题目1459:Prime ring problem(素数环问题——递归算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- hdoj 1016 Prime Ring Problem
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. #include <cstdio> #inclu ...
- HDU 1016 Prime Ring Problem(素数环问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- hdu 1016 Prime Ring Problem(dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 解决XCode 4.x SVN无法连接的问题
XCode升级到4.X版本后,确实好用了不少.但普通都存在SVN无法连接的问题.XCode4.x Source Control功能迁移到了File - Source Control目录下,也出现了一些 ...
- android之旅——开始
1.文件的读取 io流读取文件,并且显示 package com.helloword; import java.io.BufferedReader; import java.io.File; impo ...
- linux 之 yum 介绍 <转>
原文在这里 http://doophp.sinaapp.com/archives/linux/yum-setting-parameter.html 因为是程序员出身,平时虽然经常接触服务器,偶尔也会 ...
- Centos清理内存 内存回收释放及内存使用查看的相关命令
在清理前内存使用情况 free -m 用以下命令清理内存 echo 1 > /proc/sys/vm/drop_caches 清理后内存使用情况再用以下命令看看. free –m 多出很多内存了 ...
- Android上使用OpenGLES2.0显示YUV数据
在Android上用OpenGLES来显示YUV图像,之所以这样做,是因为: 1.Android本身也不能直接显示YUV图像,YUV转成RGB还是必要的: 2.YUV手动转RGB会占用大量的CPU资源 ...
- CRT detected that the application wrote to memory after end of heap buffer.
很多人的解释都不一样, 我碰到的问题是,开辟的内存空间小于操作的内存空间.也就是说,我free的内存越界了. 这是我开辟链表结构体内存的代码: PNODE Create() { int len; / ...
- 使用Instant Client配置PL/SQL Developer
之前使用PL/SQL Developer都是直接在本机安装完整版的Oracle Database,一是省事,二是可以在本机做一些demo测试:最近换了台电脑,感觉Instant Client更简单一些 ...
- Swift - 08 - 元组
//: Playground - noun: a place where people can play import UIKit // 元组就是将多个不同的值集合成一个数据 /* 元组是Object ...
- java学习——IO流
字符流的由来:其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表.获取对应的文字.在对这个文字进行操作.简单说:字节流+编码表 ---------------------------- ...
- Get URL parameters & values with jQuery
原文: http://jquery-howto.blogspot.jp/2009/09/get-url-parameters-values-with-jquery.html In this post, ...