[HDU 1016]--Prime Ring Problem(回溯)

來源:互聯網
上載者:User

標籤:

 

題目連結: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)


Problem DescriptionA 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.

 


Inputn (0 < n < 20). 


OutputThe 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. 


Sample Input68 


Sample OutputCase 1:1 4 3 2 5 61 6 5 2 3 4 Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2 


SourceAsia 1996, Shanghai (Mainland China)  


RecommendJGShining   |   We have carefully selected several similar problems for you:  1010 1241 1312 1072 1242  題目大意:有一個整數n,把從1到n的數字無重複的排列成環,且使每相鄰兩個數(包括首尾)的和都為素數,稱為素數環。     為了簡便起見,我們規定每個素數環都從1開始。有多組測試資料,每組輸入一個n(0<n<20),n=0表示輸入結束。     輸出每組第一行輸出對應的Case序號,從1開始。如果存在滿足題意敘述的素數環,從小到大輸出。 解題思路:回溯的思想就是了,一個dfs搞定,最坑爹的是,每輸完一組末尾都要加上換行,我沒加結果提交wa,明明是pe,各種改,     徹底無愛了,Orz~~~ 代碼如下:
 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4  5 #define maxn 40 6 int vis[21], x[21], T, n; 7 int prime[maxn] = { 1, 1, 0 }; 8 void init() 9 {10     int i, j;11     for (i = 2; i <= maxn; i++){12         if (!prime[i]){13             for (j = 2; i*j <= maxn; j++)14                 prime[i*j] = 1;15         }16     }17 }18 19 void dfs(int cur){20     if (cur == n&&!prime[1 + x[n - 1]]){21         for (int i = 0; i < n; i++){22             if (i) cout << ‘ ‘;23             cout << x[i];24         }25         cout << endl;26     }27     else for (int i = 2; i <= n; i++){28         if (!vis[i] && !prime[i + x[cur - 1]]){29             x[cur] = i;30             vis[i] = 1;31             dfs(cur + 1);32             vis[i] = 0;33         }34     }35 }36 37 int main(){38     init();39     while (cin >> n){40         cout << "Case " << ++T << ‘:‘ << endl;41         if (n == 1)42             cout << 1 << endl;43         else if (n & 1)44             cout << endl;45         else{46             memset(vis, 0, sizeof(vis));47             x[0] = 1;48             dfs(1);49         }50         cout << endl;//沒加這一句pe來個wa,我也是醉了,各種改,無愛了~~~~51     }52     return 0;53 }
View Code

 

[HDU 1016]--Prime Ring Problem(回溯)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.