import java.util.Scanner;
public class Main1016 {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int k=1;
while(cin.hasNext()){
int n=cin.nextInt();
int []a=new int [n];

int []color=new int [n];

for(int i=0;i<n;i++){
a[i]=i+1;
color[i]=-1;
}
//System.out.println("------------------");
//print(a);
int []parent=new int [n];
int count=0;
System.out.println("Case "+(k++)+":");
dfs(a,parent,color,0,count);

System.out.println();
}
}
private static void dfs(int[] a, int[] parent, int[] color, int g, int count) {
count++;
if(count==a.length&&isprime(a[g]+a[0])){
parent[count-1]=a[g];
print(parent);
return;
}
color[g]=1;
for(int s=0;s<a.length;s++){
if(color[s]==-1&&isprime(a[s]+a[g])){
parent[count-1]=a[g];
dfs(a,parent,color,s,count);
color[s]=-1;
}
}
}
private static boolean isprime(int i) {
for(int j=2;j*j<=i;j++){
if(i%j==0)
return false;
}
return true;
}
private static void print(int[] parent) {
for(int j=0;j<parent.length-1;j++){
System.out.print(parent[j]+" ");
}
System.out.println(parent[parent.length-1]);
}
}

这是一道简单的深搜题目。

HDU1016(bfs)的更多相关文章

  1. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  2. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  5. Sicily 1215: 脱离地牢(BFS)

    这道题按照题意直接BFS即可,主要要注意题意中的相遇是指两种情况:一种是同时到达同一格子,另一种是在移动时相遇,如Paris在(1,2),而Helen在(1,2),若下一步Paris到达(1,1),而 ...

  6. Sicily 1048: Inverso(BFS)

    题意是给出一个3*3的黑白网格,每点击其中一格就会使某些格子的颜色发生转变,求达到目标状态网格的操作.可用BFS搜索解答,用vector储存每次的操作 #include<bits/stdc++. ...

  7. Sicily 1444: Prime Path(BFS)

    题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...

  8. Sicily 1051: 魔板(BFS+排重)

    相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...

  9. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

随机推荐

  1. leetcode面试准备:Reverse Words in a String

    leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...

  2. HANA内存数据库与oracle数据库的性能比较

    链接: http://wenku.it168.com/redian/hana/ 1.传统磁盘数据库的基本访问模式.为了提高性能在产品和应用之间会加入缓存的内存区域.传统数据库性能瓶颈主要出现在一个是内 ...

  3. background image position问题

    在CSS中,背景图片的定位方法有3种: 1)关键字:background-position: top left; 2)像素:background-position: 0px 0px; 3)百分比:ba ...

  4. 通过jQuery或ScriptManager以Ajax方式访问服务

    1.客户端和服务端 服务端对外提供服务时,可以通过handler或者webservice.handler比较轻便,但是难以对外公开,只有程序员自己知道它到底做了些什么工作.webservice可以将服 ...

  5. Android中使用proguardgui混淆jar包

    本文章的前提条件是,读者已经掌握了正确导出jar包的技能. 1.拷贝Android项目中"proguard.cfg"文件到你指定的位置,并改名为"proguard.pro ...

  6. Linux Kernel 本地内存损坏漏洞

    漏洞名称: Linux Kernel 本地内存损坏漏洞 CNNVD编号: CNNVD-201310-663 发布时间: 2013-11-05 更新时间: 2013-11-05 危害等级:    漏洞类 ...

  7. [HDU 1963] Investment

    Investment Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Descrip ...

  8. ASP.NET MVC3学习心得-----表单和HTML辅助方法

    5.1表单的使用 5.1.1  action和method的特性 表单是包含输入元素的容器,包含按钮.复选框.文本框等元素,表单的这些输入元素使得用户能够向页面中输入信息,并把输入信息提交给服务器.A ...

  9. HDU-5373 The shortest problem

    The shortest problem http://acm.hdu.edu.cn/showproblem.php?pid=5373 Time Limit: 3000/1500 MS (Java/O ...

  10. POJ 1458 Common Subsequence 最长公共子序列 LCS

    LCS #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> ...