• <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>

            題目描述如下:

            ABCDE五人安排工作日程,每人每星期工作5天休息2

            1)        必須有3天所有人都要上班

            2)        每個(gè)人連續(xù)上班不超過3天,周日到周一是連續(xù)工作

            3)        AC星期三必須上班

            4)        BDE星期天都不上班

            5)        AC一星期至少見4

            6)        ABCD中每天必須至少有2人上班

            輸出所有從星期一到星期天可能的情況,每種情況間用空行隔開,0代表不上班,1代表上班。

            例:

            1 0 1 1 1 0 1

            1 1 0 1 1 1 0

            1 0 1 1 1 0 1

            1 1 0 1 1 1 0

            1 1 0 1 1 1 0

             

            結(jié)題思路:

            題目的每個(gè)解是一個(gè)5*701矩陣,枚舉每一個(gè)矩陣,然后用題目所給的條件判定即可,時(shí)間復(fù)雜度為O(2^35),明顯超時(shí)。結(jié)合題目條件“每人每星期工作5天休息2”,我們可以完成初步剪枝:對(duì)每個(gè)人的工作表,可以看成在五個(gè)1中插入兩個(gè)00的具體位置可以看成一個(gè)組合問題,既是在6個(gè)位置中選擇兩個(gè)位置(這里已經(jīng)排除兩個(gè)0相鄰的情況,因?yàn)闂l件2)。這樣,題目的復(fù)雜度為O((C26)^5),相當(dāng)小啦。除了條件1,其余的條件判斷均可在排列過程中進(jìn)行,作為剪枝只用。

             

             代碼如下:

             


            public class Main {
                
                
                
            static int mp[][] = new int[5][7];
                
            static int count;
                
            static int num[][] = {
                    
            {012345},
                    
            {012345},
                    
            {012345},
                    
            {012345},
                    
            {012345}    
                    
            /*
                     * 每個(gè)員工使用num[][]的一行完成組合,
                     * 避免別的員工組合計(jì)算時(shí)的殘留值
                     
            */

                }
            ;
                
            public static void main(String[] args) {
                    count 
            = 0;
                    DFS(
            000);
                    System.out.println(
            "count=" + count);
                    
            for(int i = 0; i < num.length; i++){
                        
            for(int n : num[i])
                            System.out.print(n 
            + " ");
                        System.out.print(
            "\n");
                    }

                    
                }

                
            static void outmp(){
                    System.out.println();
                    
            for(int i = 0; i < 5; i++){
                        
            for(int j = 0; j < 7; j++)
                            System.out.print(mp[i][j] 
            + " ");
                        System.out.println();
                    }

                }

                
            static void DFS(int c, int nowp, int left){
                    
            /*
                     * c,當(dāng)前員工(0,1,2,3,4)
                     * nowp,left用于計(jì)算組合
                     
            */

                    
            if(c >= 5){
                        
            if(R1()){
                            outmp();
                            count
            ++;
                        }

                        
            return;
                    }

                    
            if(nowp == 2){
                        makemp(c, num[c][
            0], num[c][1]);
                        
            if(!R2(c))//R2
                            return;
                        
            if(c == 0 && mp[0][2!= 1)//R3
                            return;
                        
            if(c == 2 && mp[2][2!= 1)
                            
            return;
                        
            if(c == 1 && mp[1][6!= 0)//R4
                            return;
                        
            if(c == 3 && mp[3][6!= 0)
                            
            return;
                        
            if(c == 4 && mp[4][6!= 0)
                            
            return;
                        
            if(c == 2 && !R5())//R5
                            return;
                        
            if(c == 3 && !R6())//R6
                            return;
                        
                        DFS(c 
            + 100);
                    }

                    
            else{
                        
            for(int i = left; i < num[0].length; i++){
                            swap(num[c], nowp, i);
                            DFS(c, nowp 
            + 1, i + 1);
                            swap(num[c], nowp, i);
                        }

                    }

                }

                
            static void makemp(int c, int a, int b){
                    
            for(int i = 0; i < a; i++)
                        mp[c][i] 
            = 1;
                    mp[c][a] 
            = 0;
                    
            for(int i = a + 1; i <= b; i++)
                        mp[c][i] 
            = 1;
                    mp[c][b 
            + 1= 0;
                    
            for(int i = b + 2; i < 7; i++)
                        mp[c][i] 
            = 1;
                }

                
            static boolean R1(){
                    
            int count = 0
                    
            for(int i = 0; i < 7; i++){
                        
            if(chechRol(i))
                            count
            ++;
                    }

                    
            if(count >= 3)
                        
            return true;
                    
            return false;
                }

                
            static boolean chechRol(int r){
                    
            for(int i = 0; i < 5; i++)
                        
            if(mp[i][r] != 1)
                            
            return false;
                    
            return true;
                }

                
            static boolean R2(int c){//one person
                    int count = 0;
                    
            boolean bg = false;
                    
            for(int i = 0; i < 7; i++){
                        
            if(mp[c][i] != 0){
                            
            if(bg == false){
                                count 
            = 1;
                                bg 
            = true;
                            }

                            
            else{
                                count
            ++;
                                
            if(count > 3)
                                    
            return false;
                            }

                        }

                        
            else{
                            bg 
            = false;
                        }

                    }

                    
            return true;
                }

                
            static boolean R5(){
                    
            int count = 0;
                    
            for(int i = 0; i < 7; i++){
                        
            if(mp[0][i] == 1 && mp[2][i] == 1)
                            count
            ++;
                    }

                    
            if(count >= 4)
                        
            return true;
                    
            return false;
                }

                
            static boolean R6(){
                    
            int count = 0;
                    
            for(int i = 0; i < 7; i++){
                        count 
            = 0;
                        
            for(int j = 0; j < 5; j++)
                            
            if(mp[j][i] == 1)
                                count
            ++;
                        
            if(count < 2)
                            
            return false;
                    }

                    
            return true;
                }

                
            static void swap(int a[], int n, int m){
                    
            int t = a[n];
                    a[n] 
            = a[m];
                    a[m] 
            = t;
                }

            }

             

            posted on 2013-07-06 15:43 小鼠標(biāo) 閱讀(227) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Java基礎(chǔ)練習(xí)
            <2013年7月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            常用鏈接

            隨筆分類(111)

            隨筆檔案(127)

            friends

            最新評(píng)論

            閱讀排行榜

            亚洲国产成人久久综合一区77| 污污内射久久一区二区欧美日韩 | 午夜福利91久久福利| 狠狠色丁香婷婷综合久久来 | 久久精品九九亚洲精品| 97精品国产97久久久久久免费| 99久久国产精品免费一区二区| 成人久久免费网站| 国内精品久久久久影院一蜜桃| 亚洲av伊人久久综合密臀性色| 久久九九亚洲精品| 亚洲中文久久精品无码ww16| 波多野结衣久久一区二区| 久久99精品久久久久婷婷| www亚洲欲色成人久久精品| 国产精品成人精品久久久| 久久e热在这里只有国产中文精品99 | 无码人妻久久一区二区三区蜜桃| 久久无码国产| 精品久久8x国产免费观看| 久久精品国产亚洲5555| 亚洲精品无码成人片久久| 狠狠精品干练久久久无码中文字幕| 久久综合亚洲色HEZYO社区 | 久久中文精品无码中文字幕| 亚洲精品乱码久久久久久久久久久久 | 国产农村妇女毛片精品久久| 国产毛片欧美毛片久久久| 久久久久久久99精品免费观看| 一本久久a久久精品vr综合| 精品人妻伦一二三区久久 | 久久99精品国产麻豆蜜芽| 久久久老熟女一区二区三区| 久久久久久久久久免免费精品| 99精品久久久久久久婷婷| www.久久热.com| 久久精品亚洲精品国产色婷| 香蕉久久av一区二区三区| 人妻久久久一区二区三区| 亚洲国产另类久久久精品黑人 | 欧美日韩中文字幕久久久不卡 |