Browse Source

一字摆法算法

duh 6 years ago
parent
commit
81364e7168

+ 83 - 8
src/main/java/utils/CalcYizhiArrayUtils.java

@@ -1,10 +1,6 @@
 package utils;
 
-import model.Room;
-import model.template.Template;
-
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 
 /**
@@ -17,15 +13,62 @@ public class CalcYizhiArrayUtils {
     private List<Integer> oneAlignAllLength;
     private List<Integer> twoAlignAllShortLength;
     private List<Integer> twoAlignAllLongLength;
-    public void calcYizhiWithOnlyOneTwoAlign(){
+
+    public List<List<Integer>> calcYizhiWithOnlyOneTwoAlign(){
+        List<List<Integer>> list = new ArrayList<>();
         int totalOneLenth = 0;
         for(int length : oneAlignAllLength){
             totalOneLenth += length;
         }
-        int leftForTwo = roomLength.get(3) - totalOneLenth;
-        if(leftForTwo>0){
-            System.out.println("可以一字排,方案如下:");
+        int leftForTwo = roomLength.get(2) - totalOneLenth;
+        System.out.println("totalOneLenth="+totalOneLenth);
+        System.out.println("3nd wall length="+roomLength.get(2));
+        int twoShortLength = twoAlignAllShortLength.get(0);
+        int twoLongLength = twoAlignAllLongLength.get(0);
+        if(leftForTwo>=twoLongLength &&twoLongLength>twoShortLength ){
+            System.out.println("可以一字排,长短边皆可,方案如下:");
+            //TWO at left
+            List<List<Integer>> twoAtLeftShort = addTwoAlignLeft(210);
+            list.addAll(twoAtLeftShort);
+            List<List<Integer>> twoAtLeftLong = addTwoAlignLeft(211);
+            list.addAll(twoAtLeftLong);
+            //TWO at rightt
+            List<List<Integer>> twoAtRightShort = addTwoAlignRight(220);
+            list.addAll(twoAtRightShort);
+            List<List<Integer>> twoAtRightLong = addTwoAlignRight(221);
+            list.addAll(twoAtRightLong);
+        }else if(leftForTwo>=twoShortLength){
+            System.out.println("可以一字排,只有短边,方案如下:");
+            //TWO at left
+            List<List<Integer>> twoAtLeftShort = addTwoAlignLeft(210);
+            list.addAll(twoAtLeftShort);
+            //TWO at rightt
+            List<List<Integer>> twoAtRightShort = addTwoAlignRight(220);
+            list.addAll(twoAtRightShort);
+        }else {
+            System.out.println("totalOneLenth is longer than 3nd wall");
         }
+        return list;
+    }
+
+    private List<List<Integer>> addTwoAlignRight(int numberCode) {
+        List<List<Integer>> twoAtRight = new ArrayList<>();
+        List<List<Integer>> oneArray2 = getAllArrayList(oneAlignAllLength.size());
+        twoAtRight.addAll(oneArray2);
+        for (List<Integer> twoAtLeftItem : twoAtRight){
+            twoAtLeftItem.add(numberCode);
+        }
+        return twoAtRight;
+    }
+
+    private List<List<Integer>> addTwoAlignLeft(int numberCode) {
+        List<List<Integer>> twoAtLeft = new ArrayList<>();
+        List<List<Integer>> oneArray = getAllArrayList(oneAlignAllLength.size());
+        twoAtLeft.addAll(oneArray);
+        for (List<Integer> twoAtLeftItem : twoAtLeft){
+            twoAtLeftItem.add(0,numberCode);
+        }
+        return twoAtLeft;
     }
 
     /**
@@ -69,4 +112,36 @@ public class CalcYizhiArrayUtils {
             return n*A(n-1);
         }
     }
+
+    public List<Integer> getRoomLength() {
+        return roomLength;
+    }
+
+    public void setRoomLength(List<Integer> roomLength) {
+        this.roomLength = roomLength;
+    }
+
+    public List<Integer> getOneAlignAllLength() {
+        return oneAlignAllLength;
+    }
+
+    public void setOneAlignAllLength(List<Integer> oneAlignAllLength) {
+        this.oneAlignAllLength = oneAlignAllLength;
+    }
+
+    public List<Integer> getTwoAlignAllShortLength() {
+        return twoAlignAllShortLength;
+    }
+
+    public void setTwoAlignAllShortLength(List<Integer> twoAlignAllShortLength) {
+        this.twoAlignAllShortLength = twoAlignAllShortLength;
+    }
+
+    public List<Integer> getTwoAlignAllLongLength() {
+        return twoAlignAllLongLength;
+    }
+
+    public void setTwoAlignAllLongLength(List<Integer> twoAlignAllLongLength) {
+        this.twoAlignAllLongLength = twoAlignAllLongLength;
+    }
 }

+ 34 - 3
src/test/java/utils/CalcYizhiArrayUtilsTest.java

@@ -1,22 +1,53 @@
 package utils;
 
+import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import static org.junit.Assert.*;
 
 public class CalcYizhiArrayUtilsTest {
     private CalcYizhiArrayUtils calcYizhiArrayUtils = new CalcYizhiArrayUtils();
-    @Test
+
+    @Before
+    public void setBefore() {
+        List<Integer> roomLength = new ArrayList<>();
+        roomLength.add(600);
+        roomLength.add(800);
+        roomLength.add(1000);
+        roomLength.add(500);
+        List<Integer> oneAlignAllLength = new ArrayList<>();
+        oneAlignAllLength.add(300);
+        oneAlignAllLength.add(400);
+//        oneAlignAllLength.add(300);
+        List<Integer> twoAlignAllShortLength = new ArrayList<>();
+        twoAlignAllShortLength.add(300);
+        List<Integer> twoAlignAllLongLength = new ArrayList<>();
+        twoAlignAllLongLength.add(500);
+        calcYizhiArrayUtils.setRoomLength(roomLength);
+        calcYizhiArrayUtils.setOneAlignAllLength(oneAlignAllLength);
+        calcYizhiArrayUtils.setTwoAlignAllShortLength(twoAlignAllShortLength);
+        calcYizhiArrayUtils.setTwoAlignAllLongLength(twoAlignAllLongLength);
+    }
+
+    @Ignore
     public void getAllArrayList() {
         System.out.println(Integer.MAX_VALUE);
         System.out.println(Integer.MIN_VALUE);
-        for(int i =1;i<16;i++){
-            System.out.println("A("+i+")="+calcYizhiArrayUtils.A(i));
+        for (int i = 1; i < 16; i++) {
+            System.out.println("A(" + i + ")=" + calcYizhiArrayUtils.A(i));
         }
 //        int n = 6;
 //        List<List<Integer>> list = calcYizhiArrayUtils.getAllArrayList(n);
 //        list.forEach(x-> System.out.println(x));
     }
+
+    @Test
+    public void calcYizhiWithOnlyOneTwoAlign() {
+        List<List<Integer>> list = calcYizhiArrayUtils.calcYizhiWithOnlyOneTwoAlign();
+        list.forEach(x -> System.out.println(x));
+    }
 }