hdu 1016 Prime Ring Problem

简单的搜索题,随便看了下人家的代码之后,一次AC,哈哈哈


/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2011 panyanyany All rights reserved. URL : http://acm.hdu.edu.cn/showproblem.php?pid=1016 Name : 1016 Prime Ring Problem Date : Wednesday, August 10, 2011 Time Stage : 2 hours less Result: 4378651 2011-08-10 16:13:31 Accepted 1016 171MS 204K 1390 B C++ pyy Test Data: Review: //----------------------------------------------------------------------------*/ #include #include #define MAXSIZE 40 int n, len, tcase ; int prime[MAXSIZE], ans[MAXSIZE], used[MAXSIZE] ; void dfs (int a) { ans[len++] = a ; used[a] = 1 ; int i ; if (len - 1 == n) { if (prime[ans[1]+a]) { for (i = 1 ; i < n ; ++i) { printf ("%d ", ans[i]) ; } printf ("%d\n", ans[i]) ; } } else { for (i = 2 ; i <= n ; ++i) { if (prime[i + a] && !used[i]) { dfs (i) ; } } } --len ; used[a] = 0 ; } int main () { int i, j ; memset (prime, 1, sizeof (prime)) ; for (i = 2 ; i < MAXSIZE ; ++i) { if (prime[i]) for (j = i ; j*i < MAXSIZE ; ++j) prime[j*i] = 0 ; } tcase = 0 ; while (scanf ("%d", &n) != EOF) { memset (used, 0, sizeof (used)) ; len = 1 ; ++tcase ; printf ("Case %d:\n", tcase) ; dfs (1) ; puts ("") ; } return 0 ; }

你可能感兴趣的:(hdu 1016 Prime Ring Problem)