杭电 1016 Prime Ring Problem
2013-12-19 19:54
295 查看
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18313 Accepted Submission(s): 8197
[align=left]Problem Description[/align]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.
[align=left]Input[/align]
n (0 < n < 20).
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
6 8
[align=left]Sample Output[/align]
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
这道题之前做过一遍,那一次程序效率比现在这个还要高,但原来怎么做的我已经忘了,遂又找了个时间做了一遍。
由于是做过的第一道DFS(深度优先搜索)的搜索题,所以一定要弄熟。
1 #include <iostream> 2 using namespace std; 3 int a[21][21]; 4 int q[21]; //定义一个队列q,存储当前确定的素数环 5 int main() 6 { 7 bool boo_prime(int); 8 void f(const int ,int ,int q[21]); 9 int i,j,n,digit; 10 int count=1; 11 //计算20以内的素数矩阵,存储到二维数组a中 12 for(i=1;i<=20;i++) 13 for(j=1;j<=20;j++) 14 if(boo_prime(i+j)) 15 a[i][j]=1; 16 else 17 a[i][j]=0; 18 a[1][1]=0; 19 //循环输入n,输出每一个n所有符合的素数环 20 while(cin>>n){ 21 digit=1; 22 cout<<"Case "<<count<<":"<<endl; 23 f(n,digit,q); //调用递归函数f(),输出每种符合的情况 24 cout<<endl; 25 count++; 26 } 27 return 0; 28 } 29 bool boo_prime(int n) //判断一个整数n是不是素数 30 { 31 int i; 32 //判断n是不是素数 33 if(n==1) return false; 34 if(n==2) return true; 35 for(i=2;i<n;i++){ 36 if(n%i==0) return false; 37 } 38 return true; 39 } 40 void f(const int n,int digit,int q[21]) 41 { 42 if(digit==1){ //如果确认到第1位 43 q[1]=1; 44 f(n,digit+1,q); 45 } 46 else if(digit<=n){ //如果确认到第2~n位 47 int i,j; 48 for(j=1;j<=n;j++){ //循环确定当前位数上的数字是谁 49 if(a[q[digit-1]][j]==1){ //上一个确认的数字和j之和是否为素数。若是素数,继续向下判断 50 bool boo=true; 51 //如果这个数字就是j的话,它需要不能和前面确定的数字重复 52 for(i=1;i<digit;i++) 53 if(j==q[i]){ //如果和前面确定的数字重复的话,令boo=false,说明这种情况不可能;若没有重复,boo默认=true,即下个数字可以是j 54 boo=false; 55 break; 56 } 57 if(boo){ //如果这个数字可以是j 58 q[digit]=j; //确认这个数字就是j 59 if(digit==n){ 60 if(a[j][1]==1) 61 f(n,digit+1,q); 62 } 63 else 64 f(n,digit+1,q); 65 } 66 } 67 } 68 } 69 else if(digit>n){ //搜索到一种符合的情况,输出 70 int i; 71 for(i=1;i<=n;i++) 72 if(i==n) cout<<q[i]<<endl; 73 else cout<<q[i]<<' '; 74 } 75 }
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
8262140 | 2013-05-08 17:24:05 | Accepted | 1016 | 890MS | 388K | 2263 B | G++ | freecode |
相关文章推荐
- 第一个小项目 - 去掉指定文件中的注释
- 杭电 1175 连连看
- 【Java笔记】C++与Java的对比
- UESTC 1900 倒推数组 (找规律)
- 字节流与位流的相互转换实现
- BOXI R2 SI_LAST_RUN_TIME的刷新
- Python基础教程---读书笔记二
- SEO最终是为了什么?
- C文件操作之--删除某文件中指定行
- hadoop生态系统
- SMTP命令行
- iPhone开发-创建、读取、写入文件 今天的东西是如何在iPhoneOS下创建、删除、读取、写入文件 创建与删除: //创建文件管理器 NSFileManager *fileManager =
- (转)40多个关于人脸检测/识别的API、库和软件
- JAVA异常机制介绍/如何正确的进行JAVA异常处理
- Struts注解,Spring注解,Hibernate注解
- 计算机网络学习-传输层
- 菜鸟Android学习之路14——带图标的ListView组件
- OCR学习之一:OCR简介
- 看看机器学习都有哪些比较成熟的应用
- Android(2)—Mono For Android App版本自动更新