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.
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.
8
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std;
int n;
int vis[25];
int prime[38]={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};
int ans[25];
void print(int m)
{
int i;
printf("%d",ans[1]);
for(i=2;i<=n;i++)
{
printf(" %d",ans[i]);
}
printf("\n");
}
void dfs(int point)
{
if(point==n && prime[ans[point]+ans[1]])
{
print(point);
}
else
{
for(int j=2;j<=n;j++)
{
if(!vis[j]&& prime[ans[point]+j])
{
ans[point+1]=j;
vis[j]=1;
dfs(point+1);
vis[j]=0;
}
}
}
}
int main()
{
int cas=0;
while(scanf("%d",&n)!=EOF)
{
cas++;
memset(vis,0,sizeof(vis));
vis[1]=1;
ans[1]=1;
printf("Case %d:\n",cas);
dfs(1);
printf("\n");
}
}
Prime Ring Problem的更多相关文章
-
uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
-
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 ...
-
杭电oj 1016 Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
-
hdu 1016 Prime Ring Problem(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
-
HDU1016 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 ...
-
UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
-
HDU 1016 Prime Ring Problem (回溯法)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
-
Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black
Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
随机推荐
-
ASP.NET WebAPi之断点续传下载(中)
前言 前情回顾:上一篇我们遗留了两个问题,一个是未完全实现断点续传,另外则是在响应时是返回StreamContent还是PushStreamContent呢?这一节我们重点来解决这两个问题,同时就在此 ...
-
CSS实现元素水平/垂直居中的方法
首先,我们来了解水平居中,它有很多种方法,我们暂时先来了解其中的几种: 1. 在实现方案中,我们最熟悉的莫过于给元素定义一个宽度,然后使用margin: 1 2 3 4 body{ wi ...
-
Codeforces Round #339 (Div.2)
A. Link/Cut Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
-
com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
问题描述:最简单的数据库连接报错,到主机 的 TCP/IP 连接失败.(win 7 操作系统) 错误信息: com.microsoft.sqlserver.jdbc.SQLServerExcep ...
-
Vim,一个开放源代码的文本编辑器(转)
Vim,http://linux.21ds.net/2002/03/13/0268dc26fd9c725c23dae68d797935f3/ 作者:Bram Moolenaar 翻译:slimzhao ...
-
Telerik_2012_Q3 RadGrid 汉化
ChineseRadGridLocalizationProvider.cs using System; using System.Collections.Generic; using System.L ...
-
Lucene全文搜索 分组,精确查找,模糊查找
http://zm603380946.iteye.com/blog/1827318 完全个人理解,如有更好的方法,欢迎一起讨论 LuceneUtils.java package com.zbiti.l ...
-
推荐一款基于bootstrap的漂亮的前端模板—inspinia_admin
首先给出Demo网址:http://cn.inspinia.cn inspinia admin 最新版 bootstrap 完全响应式后台管理模板,采用扁平化设计.使用Bootstrap 3+ Fra ...
-
android上的bin/sbin/xbin等各种目录
1. /system是用于存储 由AOSP构建生成的 不可变组件的 主要Android目录.这包括本机二进制文件,本机库,框架包和存储主要的应用程序.它通常是从根文件系统的单独映像中以只读方式挂载的, ...
-
OAuth2 token
1.资源服务器 package com.ruhuanxingyun.config; import com.fasterxml.jackson.databind.ObjectMapper; import ...