阿1111

 

public class LayeredTest {

 

    public static void main(String[] args) {
        int[] a = {50,600,700,1000};
        List<String> resolveList = resolve(4, a);


        // 输出结果
        for (String temp : resolveList) {
            StringBuilder sb = new StringBuilder();
            char[] chars = temp.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                if (sb.length() > 0) {//该步即不会第一位有逗号,也防止最后一位拼接逗号!
                    sb.append(",");
                }
                sb.append(chars);
            }
            System.out.println(sb);
        }

    }

    // 所有可能的组合
    private static List<String> allCaseList = new ArrayList<>();

    /**
     * 获取所有排列组合,去除不满足条件的组合(不能移动的版号的索引位置,要大于该  版号+1 所在索引位置)
     * n : 层板的数量
     * zs : 层板原始位置集合
     * list : 层板排序集合
     * */
    private static List<String> resolve(int n,int[] zs) {

        // 确定每一块层板的位置,avgHeight:每一层的平均高度
        int avgHeight = 2000/(n+1);
        // 每块木板位置的集合
        List<Integer> boardLocationList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            boardLocationList.add(avgHeight*(i+1));
//            System.out.println(avgHeight*(i+1));
        }
        // 初始化array: 层板号数组
        String[] array = new String[n];
        for (int i = 0; i < n; i++) {
            array = String.valueOf(i+1);
        }

        //  获取所有层板号的移动顺序的组合,存入成员变量  allCaseList 中
        arrangeAll(Arrays.asList(array),"",n);
        //  获取不满足移动条件的层板号集合
        List<Integer> noMoveBoardList = noMoveBoard(boardLocationList, zs);
//        System.out.println(noMoveBoardList.toString());
        // 删除,不符合条件的排列
        List<String> correctCaseList = removeErrorCase(noMoveBoardList);
        // 对结果进行从小到大排序
        Collections.sort(correctCaseList);


        return correctCaseList;
    }


    /**
     *  删除不满足条件的排列组合
     *  noMoveBoardList : 不满足移动条件的层板号集合
     *  return : 返回正确的排序集合
     * */
    public static List<String> removeErrorCase(List<Integer> noMoveBoardList) {
        List resultList = new LinkedList(allCaseList);
        //待删除元素集合
        List<String> tempList = new ArrayList<>();

        for (int i = 0; i < noMoveBoardList.size(); i++) {
            // 不能移动的板子
            Integer temp = noMoveBoardList.get(i);
            // temp 后面的板子
            Integer tempNext = temp + 1;
            for (String str: allCaseList) {
                int maxTemp = str.indexOf(temp.toString());
                int minTemp = str.indexOf(tempNext.toString());
                if (maxTemp < minTemp) {
                    tempList.add(str);
                }
            }
        }

        if (tempList.size() != 0) {
            for(String removeStr: tempList) {
                resultList.remove(removeStr);
            }
        }
        return resultList;

    }

    /**
     * 获取所有层板号的移动顺序的组合
     * array : 层板号数组
     * prefix : 层板号排列组合的一种情况
     * */
    public static void arrangeAll(List array, String prefix,int n){
        if (prefix.length() == n) {
            allCaseList.add(prefix);
        }
        for (int i = 0; i < array.size(); i++) {
            List temp = new LinkedList(array);
            arrangeAll(temp, prefix + temp.remove(i),n);
        }
    }

    /**
     * 获取不满足移动条件的层板号
     * boardLocationList : 层板目标位置集合
     * zs : 层板原始位置集合
     * return : 不满足移动条件的层板号集合
     * */
    public static List<Integer> noMoveBoard(List<Integer> boardLocationList,int[] zs) {
        List<Integer> resultList = new ArrayList<>();
        for (int i = 0; i < boardLocationList.size(); i++) {
            if (i == (zs.length-1)) {
                break;
            }
            if (boardLocationList.get(i) >= zs[i+1]) {
                resultList.add(i+1);
            }
        }
        return resultList;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值