HDOJ 1016 HDU 1016 Prime Ring Problem ACM 1016 IN HDU
2010-08-18 18:41
399 查看
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1016题目描述:
Total Submission(s): 6150 Accepted Submission(s): 2745
[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.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/427b1dd2282412709d583cc17eda983e.gif)
[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]
[align=left]Sample Output[/align]
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6150 Accepted Submission(s): 2745
[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.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201604/427b1dd2282412709d583cc17eda983e.gif)
[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 题目, 不需要 什么剪枝, 直接 穷举 + 回溯 就OK了, 不过值得一提的是,这题输出很BT, 一般的 前后 输出 回车 , 第一个回车用 if( n == 1 ) 回车; 来做PE了好几次, 最后直接在程序最后 输出2个回车符竟然就A了, YM啊........... 代码如下:/* MiYu原创, 转帖请注明 : 转载自 ______________白白の屋 http://www.cppblog.com/MiYu Author By : MiYu Test : Program : */ #include <iostream> using namespace std; bool prim[25]; int res[25]; bool hash[25]; int N; void setPrim () { memset ( prim, 0, sizeof ( prim ) ); prim[2] = prim[3] = prim[5] = prim[7] = prim[11] = prim[13] = prim[17] = prim[19] = prim[23] = true; } bool DFS ( int num , int n ) { res = num; if ( n > N ) { return false; } if ( n == N - 1 ) { for ( int i = 2; i <= N; ++ i ) { if ( prim[num + i] && prim[ i + 1 ] && !hash[i] ) { res[n+1] = i; for ( int i = 1; i <= N; ++ i ) printf ( i == 1 ? "%d" : " %d",res[i] ); putchar ( '\n' ); } } } for ( int i = 2; i <= N; ++ i ) { if ( prim[ num + i ] && !hash[i] ) { hash[i] = true; DFS ( i, n + 1 ); hash[i] = false; } } return false; } int main () { setPrim (); int ca = 1; while ( cin >> N ) { sizeof ( hash, 0 , sizeof ( hash ) ); printf ( "Case %d:\n",ca++ ); hash[1] = true; DFS ( 1, 1 ); putchar ( '\n' ); } return 0; }
相关文章推荐
- [ACM] hdu 1016 Prime Ring Problem (DFS)
- [ACM] hdu 1016 Prime Ring Problem (DFS)
- [ACM] hdu 1016 Prime Ring Problem (DFS)
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
- 搜索专题(DFS)HDU 1016-Prime Ring Problem
- HDU 1016-Prime Ring Problem
- hdu 1016 Prime Ring Problem(深度优先搜索)
- HDU 1016 Prime Ring Problem(水题纯暴)
- HDU 1016 Prime Ring Problem(DFS)
- hdu 1016 Prime Ring Problem
- HDU 1016 Prime Ring Problem(DFS)
- HDU——1016 Prime Ring Problem
- HDU 1016 Prime Ring Problem (经典DFS)
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
- HDU 3468 HDOJ 3468 A Simple Problem with Integers ACM 3468 IN HDU
- HDU 1016 Prime Ring Problem
- 【HDU 1016】Prime Ring Problem —— DFS
- 【暴力搜索】[HDU 1016]Prime Ring Problem
- Prime Ring Problem HDU - 1016
- hdoj problem 1016 Prime Ring Problem(素数环&&素数问题+DFS(回溯法))