简单的深度搜索--素环问题
来源:互联网 发布:工作计划软件 编辑:程序博客网 时间:2024/05/21 13:50
杭电地址:http://acm.hdu.edu.cn/showproblem.php?pid=1016
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13751 Accepted Submission(s): 6276
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
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
Source
Asia 1996, Shanghai (Mainland China)
Recommend
JGShining
#include <stdio.h>#include <string.h>int prime[40]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0}; int Circle[20];bool Visit[20],flag;int n,t=1;void Dfs(int x,int y){ int i; if(y==n+1&&prime[Circle[1]+x]==1) { if(!flag) { printf("Case %d:\n",t++); flag=true; } for( i=1;i<y-1;i++) printf("%d ",Circle[i]); printf("%d\n",Circle[i]); return ; } for( i=2;i<=n;i++) if(!Visit[i]&&prime[x+i]==1) { Circle[y++]=i; Visit[i]=true; Dfs(i,y); y--; Visit[i]=false; }}int main(){ while(scanf("%d",&n)==1) { flag=false; memset(Visit,false,sizeof(Visit)); Visit[1]=true; Circle[1]=1; Dfs(1,2); printf("\n"); } return 0;}
- 简单的深度搜索--素环问题
- 迷宫问题—简单深度搜索
- 素数环问题,深度搜索
- 简单的深度优先搜索HDU1045
- 简单深度优先搜索
- 经典的四皇后问题深度搜索
- poj 1321 棋盘问题 简单深度优先搜索
- poj1248深度搜索简单题
- 深度优先搜索简单入门
- pku 1164 解题报告 简单的深度优先搜索
- C语言对深度搜索的简单应用(Oil Deposits
- POJ 2386 Lake Counting(简单的深度搜索)
- 图的深度广度搜索简单训练:Course Schedule
- 城堡问题 ------ 深度优先搜索
- HDU 1010 深度搜索问题
- 棋盘问题(深度搜索)
- 深度搜索的变形
- 图的深度搜索
- windows 核心编程之8 用户模式下的线程同步
- mysql 主从复制
- 《Cpp primer》4th 翻译有误的一个地方
- 一.顺序存储线性表
- Android 压缩解压zip文件
- 简单的深度搜索--素环问题
- 动态MBean:DynamicMBean
- 改变Apache端口等配置修改方法
- SecurityException: java.lang.SecurityException
- 多比特树与RFC详解
- RegExp.$1--RegExp.$9
- 校招季——Python笔记一
- jdk和maven安装后遇到的一些问题
- 要再次显示该网页 Internet Explorer 需要重新发送您以前提交的信息