hdu1016(简单深搜)

这是一个简单深搜问题,要求相邻的数之间相加为素数。采用深搜,把满足条件的值放在parent[]中。最后输出parent[].

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 {
	static int k=0;
	public static void main(String[] args) {
		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[] parent = new int[n]; //用来装结果的数组
			int count = 0;   //用来记录搜索个数
			System.out.println("Case "+(++k)+":");
			// 初始化数据
			for (int i = 0; i < n; i++) {
				a[i] = i + 1;
				color[i] = -1;//把未搜索的标记为-1
				parent[i] = -1;
			}
			dfs(a, color, parent, 0, count);
			System.out.println();
		}
	}

	private static void dfs(int[] a, int[] color, int[] parent, int u, int count) {
		color[u] = 1; //把搜索过的标记为1
		count++;
		//递归跳出的条件:次数到达了给定的的个数即数组a[]的长度;最后一个数和第一个数相加满足是素数
		if (count == a.length && isPrime(a[u] + a[0])) {
			parent[count - 1] = a[u];//把最后一个结果放到parent中
			print(parent);//输出结果
			return;
		}
		for (int v = 0; v < a.length; v++) {
			//满足color值为-1和当前值和下一个值相加为素数进入dfs
			if (color[v] == -1 && isPrime(a[v] + a[u])) {
				parent[count - 1] = a[u];//把满足的值放在结果数组中
				dfs(a, color, parent, v, count);
				color[v] = -1;//还原标记
			}
		}
	}
    //输出结果
	private static void print(int[] parent) {
		for (int i = 0; i < parent.length; i++) {
			if (i < parent.length - 1) {
				System.out.print(parent[i] + " ");
			} else {
				System.out.println(parent[i]);
			}
		}
	}

	//判断是否为素数
	private static boolean isPrime(int num) {
		for (int i = 2; i * i <= num; i++) {
			if (num % i == 0) {
				return false;
			}
		}
		return true;
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-23 03:08:56

hdu1016(简单深搜)的相关文章

fzu 1920 Left Mouse Button(简单深搜题)

题目地址:http://acm.fzu.edu.cn/problem.php?pid=1920 题目大意是给定一个n*n的图,模拟扫雷游戏,0代表没有雷区,1代表附近九宫格内只有一个雷-- 如果不懂的话去玩下扫雷,挺好玩的,99个雷的玩了好几百盘才赢了一次............ 此题假设神操作,点不到雷,问你最少要多少下才可以把图中非雷的点完,怎么样才最快呢? 当然先点0啦,,,,,,,, 好吧,不废话了..... ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1

ZOJ Seeding 2100【简单深搜】

Seeding Time Limit: 2 Seconds      Memory Limit: 65536 KB It is spring time and farmers have to plant seeds in the field. Tom has a nice field, which is a rectangle with n * m squares. There are big stones in some of the squares. Tom has a seeding-ma

简单深搜:POJ1546——Sum it up

结束了三分搜索的旅程 我开始迈入深搜的大坑.. 首先是一道比较基础的深搜题目(还是很难理解好么) POJ 1564 SUM IT UP 大体上的思路无非是通过深搜来进行穷举.匹配 为了能更好地理解深搜 可以尝试去画一下二叉树理解一下,查看遍历的路径 代码还是百度到别人的自己再参悟- -佩服别人的功底啊 先上代码: /*POJ 1546 Sum it up*/ # include<iostream> # include<algorithm> # include<cstdio&g

hdu 1241 Oil Deposits(八方向简单深搜,对新手挺经典)

Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12461 Accepted Submission(s): 7245 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground

简单深搜(poj 3009)

题目链接:http://poj.org/problem?id=3009 题目:冰壶撞向目的地,只有遇到"1"才能停下来,并且把"1"撞成"0".只能横冲直撞,不允许蛇皮走位等等骚操作.从"2"要撞到"3",周围有"0",才能向有"0"的地方滑.运动员只能推十次,问最少要多少次才到"3"? 用深搜遍历每一个方向. 1 #include<stdi

poj1321棋盘简单深搜

这个题确实有点简单,看清楚不能同列同行就好了,用一个一维数组标记哪一列用了往下搜就好了 #include<stdio.h> int flag[8]={1,1,1,1,1,1,1,1},i,j,n,k,num=0; char arr[10][11]; void dfs(int a,int b) { int ii,jj; if(b==0) { num++; return ; } for(ii=a+1;ii<n;ii++) for(jj=0;jj<n;jj++) { if(flag[jj

HDOJ1015(简单深搜)

简单的深度优先搜索.求最大字典序,注意要先排序. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int SIZE=13; char a[5]; int vis[SIZE*2+10]; bool cmp(char a,char b) { return b > a; } int npow(int x,i

nyoj587 hdu1045 简单深搜

#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<map> #include<list> #include<set> #include<stack> #include<cstring> #include<string> #include<algorithm> #inclu

简单深搜

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=27 #include <stdio.h> #include <string.h> int t; int r,c; int maps[105][105]; int color[105][105]; int to[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; void dfs(int i,int j) { if(maps[i][j]==1&&a