(动态规划)电路布线问题

public class MNset {

    public static void mnset(int[] c, int[][] size) {
        int n = c.length - 1;
        for (int j = 0; j < c[1]; j++) {
            size[1][j] = 0;  // 下面没有接线柱的时候,都为0
        }
        for (int j = c[1]; j <= n; j++) {
            size[1][j] = 1; // 有且一根接线柱连上,后面的也是一根
        }
        for (int i = 2; i <= n; i++) { // 超过两根接线柱的时候
            for (int j = 0; j < c[i]; j++) {
                size[i][j] = size[i - 1][j]; // 往上看,因为这根线还没连上
            }
            for (int j = c[i]; j <= n; j++) {// 连上了,看看要还是不要
                size[i][j] = Math.max(size[i - 1][j],size[i - 1][c[i] - 1] + 1);
            }
        }
        // 最后返回
        size[n][n] = Math.max(size[n - 1][n],size[n - 1][c[n] - 1] + 1);
    }

    // 构造最优解
    public static int traceback(int[] c, int[][] size, int[] net) {
        int n = c.length - 1;
        int j = n;
        int m = 0;
        for (int i = n; i > 1; i--) {
            if (size[i][j] != size[i - 1][j]) {
                net[m++] = i;
                j = c[i] - 1;
            }
            if (j >= c[i])
                net[m++] = 0;
        }
        return m;
    }

    public static void main(String[] args) {
        int[] c = {0,6,8,12,2,1,4,5,3,11,7,10,9,13};
        int[][] size = new int[c.length][c.length];
        MNset.mnset(c,size);
        System.out.println(Arrays.toString(size[0]));
        System.out.println(Arrays.toString(size[1]));
        System.out.println(Arrays.toString(size[2]));
        System.out.println(Arrays.toString(size[3]));
        System.out.println(Arrays.toString(size[4]));
        System.out.println(Arrays.toString(size[5]));
        System.out.println(Arrays.toString(size[6]));
        System.out.println(Arrays.toString(size[7]));
        System.out.println(Arrays.toString(size[8]));
        System.out.println(Arrays.toString(size[9]));
        System.out.println(Arrays.toString(size[10]));
        System.out.println(Arrays.toString(size[11]));
        System.out.println(Arrays.toString(size[12]));
        System.out.println(Arrays.toString(size[13]));


        int[] net = new int[c.length];
        MNset.traceback(c, size, net);
        System.out.println("------------result---------------");
        System.out.println(Arrays.toString(net));

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值