• <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)        每個人連續(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é)題思路:

            題目的每個解是一個5*701矩陣,枚舉每一個矩陣,然后用題目所給的條件判定即可,時間復(fù)雜度為O(2^35),明顯超時。結(jié)合題目條件“每人每星期工作5天休息2”,我們可以完成初步剪枝:對每個人的工作表,可以看成在五個1中插入兩個00的具體位置可以看成一個組合問題,既是在6個位置中選擇兩個位置(這里已經(jīng)排除兩個0相鄰的情況,因為條件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}    
                    
            /*
                     * 每個員工使用num[][]的一行完成組合,
                     * 避免別的員工組合計算時的殘留值
                     
            */

                }
            ;
                
            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用于計算組合
                     
            */

                    
            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) 閱讀(220) 評論(0)  編輯 收藏 引用 所屬分類: Java基礎(chǔ)練習(xí)
            <2013年7月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            常用鏈接

            隨筆分類(111)

            隨筆檔案(127)

            friends

            最新評論

            閱讀排行榜

            久久国产午夜精品一区二区三区| 亚洲欧洲精品成人久久曰影片| 久久精品国产日本波多野结衣| 精品久久国产一区二区三区香蕉| 亚洲AV无码久久精品成人| 综合久久给合久久狠狠狠97色| 久久精品无码一区二区三区免费 | 亚洲精品tv久久久久久久久| 亚洲国产成人久久综合一区77| 久久夜色精品国产www| 国产精品99久久久久久董美香| 亚洲午夜精品久久久久久人妖| 久久精品国产99国产精偷| 国产一级做a爰片久久毛片| 久久精品国产精品亚洲精品| 欧美亚洲国产精品久久蜜芽| 99精品久久久久久久婷婷| 久久精品国产精品亜洲毛片| 久久精品国产亚洲AV不卡| 久久夜色撩人精品国产| 久久天天躁狠狠躁夜夜avapp| 婷婷久久久亚洲欧洲日产国码AV| 久久综合给合久久狠狠狠97色| 72种姿势欧美久久久久大黄蕉| 9191精品国产免费久久| 久久精品国产99国产精品| 亚洲国产精品嫩草影院久久 | 91精品国产91久久久久久| 精品久久综合1区2区3区激情| 香蕉aa三级久久毛片| 亚洲国产精品无码久久久不卡 | 伊人久久综合成人网| 99久久精品日本一区二区免费 | 久久亚洲精品无码VA大香大香| 国产偷久久久精品专区| 国产精品久久久久aaaa| 久久精品18| 久久亚洲精品人成综合网| 久久久久久青草大香综合精品| 99久久99久久精品国产片果冻| 国产精品视频久久|