• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            HDOJ 1016 Prime Ring Problem

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


             

            Input
            n (0 < n < 20).
             

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

            Sample Input
            6
            8
             

            Sample Output
            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
                素數(shù)環(huán):由1到n個數(shù)構(gòu)成一個環(huán),其中環(huán)內(nèi)任意2個相鄰的數(shù)字之和是素數(shù)。
                比較經(jīng)典的搜索題,由于n<20,可以先預(yù)處理出前40個自然數(shù)中的素數(shù),然后深搜某個位置的未被訪問過的數(shù)字和它相鄰位置的數(shù)字之和是否為素數(shù),搜索退出的條件為最后一個位置的數(shù)字circle[n]+1是否為素數(shù)。一次搜索完成后,要回溯,否則只會輸出一組解。
            #include <iostream>
            using namespace std;

            const int MAXN = 41;
            bool visit[MAXN];
            int n,p[MAXN],circle[MAXN];

            void prime(){
                
            int i,j;
                memset(p,
            true,sizeof(p));
                
            for(i=2;i<MAXN;i++)
                    
            for(j=2;i*j<MAXN;j++)
                        p[i
            *j]=false;
            }

            void dfs(int c,int cnt){
                
            if(cnt==&& p[circle[1]+circle[n]]){
                    
            for(int i=1;i<n;i++)
                        printf(
            "%d ",circle[i]);
                    printf(
            "%d\n",circle[n]);
                }

                
            for(int i=c+1;i<MAXN;i++)
                    
            if(p[i] && i-c<=&& !visit[i-c]){
                        circle[cnt
            +1]=i-c;
                        visit[i
            -c]=true;
                        dfs(i
            -c,cnt+1);
                        visit[i
            -c]=false;
                    }

            }

            int main(){
                
            int c=1;
                prime();
                
            while(scanf("%d",&n)!=EOF){
                    memset(visit,
            false,sizeof(visit));
                    printf(
            "Case %d:\n",c++);
                    visit[
            1]=true,circle[1]=1;
                    dfs(
            1,1);
                    printf(
            "\n");
                }

                
            return 0;
            }

            posted on 2009-05-24 13:37 極限定律 閱讀(1171) 評論(1)  編輯 收藏 引用 所屬分類: ACM/ICPC

            評論

            # re: HDOJ 1016 Prime Ring Problem 2009-08-04 09:53

            謝了 ?。。。。。。。。。?  回復(fù)  更多評論   

            <2009年4月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久国产精品三级网| 久久久久久久久无码精品亚洲日韩| 久久精品中文无码资源站| 热久久这里只有精品| 72种姿势欧美久久久久大黄蕉| 最新久久免费视频| 亚洲欧美另类日本久久国产真实乱对白 | 久久免费大片| 久久国产香蕉视频| 精品久久久久一区二区三区| 91久久精品国产91性色也| 久久久久四虎国产精品| 久久精品国产99国产精偷| 久久青青草原精品影院| 国产精品成人精品久久久| 国产真实乱对白精彩久久| 精品国产乱码久久久久久浪潮| 精品久久久久久久久久中文字幕| 国产三级精品久久| 亚洲国产精品成人久久蜜臀 | 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 久久久无码精品亚洲日韩蜜臀浪潮| 久久人人妻人人爽人人爽| 精品久久777| 久久国产热这里只有精品| 午夜精品久久影院蜜桃| 久久久久高潮综合影院| 成人国内精品久久久久影院| 一本久久久久久久| 久久久久免费精品国产| 精品国产一区二区三区久久久狼| 久久综合九色综合精品| 亚洲国产成人久久综合区| 亚洲va久久久噜噜噜久久狠狠 | 亚洲国产成人久久笫一页| 老男人久久青草av高清| 成人久久久观看免费毛片| 人妻中文久久久久| 97久久精品国产精品青草| 亚洲?V乱码久久精品蜜桃 | 久久人人爽人人爽人人片AV不|