Lose a piece of pawn

Source: Internet
Author: User

Missing pieces problem public class throwchessproblem{//(1) Recursive way of calculation (time complexity is O (n!)) public static int Solution1 (int nlevel,int kchess) {if (nlevel<1| |
         kchess<1) {return 0;
    Return Process1 (nlevel,kchess);
        public static int Process1 (int nlevel,int kchess) {if (nlevel==0) {return 0;
        } if (kchess==1) {return nlevel;
        int min=integer.max_value;
           for (int i=1;i!=nlevel+1;i++) {if (i==nlevel) {}//In two cases I-tier pieces are broken or not broken.

        Min=math.min (Min,math.max (Process1 (i-1,kchess-1), Process1 (nlevel-i,kchess)));
    return min+1; }//(2) Dynamic programming approach (Time complexity O (n*n*k)) public static int Solution2 (int nlevel,int kchess) {if (nlevel<1|
         |kchess<1) {return 0;
         } if (kchess==1) {return nlevel; }//Generate Dynamic planning table Int[][]dp=new int[nlevel+1][KCHESS+1];
         for (int i=1;i!=nlevel+1;i++) {dp[i][1]=i;
                  for (int i=1;i!=dp.length;i++) {for (int j=2;j!=dp[0].length;j++) {
                  int min=integer.max_value;
                  for (int k=1;k!=i+1;k++) {min=math.min (Min,math.max (DP[K-1][J-1],DP[I-K][J)));
              } dp[i][j]=min+1;
    } return dp[nlevel][kchess];
            //Improved dynamic planning 1 public static int Solution3 (int nlevel, int kchess) {if (Nlevel < 1 | | Kchess < 1) {
        return 0;
        } if (kchess = = 1) {return nlevel;
        } int[] Prearr = new Int[nlevel + 1];
        int[] Curarr = new Int[nlevel + 1];
        for (int i = 1; I!= curarr.length i++) {Curarr[i] = i;
            for (int i = 1; I!= kchess i++) {int[] tmp = Prearr; Prearr= Curarr;
            Curarr = tmp;
                for (int j = 1; J!= Curarr.length + +) {int min = Integer.max_value;
                for (int k = 1; k!= j + 1; k++) {min = math.min (min, Math.max (prearr[k-1), curarr[j-k]);
            } Curarr[j] = min + 1;
    } return curarr[curarr.length-1];
            //Improved dynamic planning 2 public static int solution4 (int nlevel, int kchess) {if (Nlevel < 1 | | Kchess < 1) {
        return 0;
        } if (kchess = = 1) {return nlevel;
        } int[][] dp = new Int[nlevel + 1][kchess + 1];
        for (int i = 1; I!= dp.length i++) {dp[i][1] = i;
        } int[] Cands = new int[kchess + 1];
            for (int i = 1; I!= dp[0].length i++) {dp[1][i] = 1;
        Cands[i] = 1;
for (int i = 2; i < Nlevel + 1; i++) {for (int j = kchess; j > 1; j--) {                int min = Integer.max_value;
                int minenum = Cands[j]; int maxenum = j = = kchess?
                I/2 + 1:cands[j + 1];
                    for (int k = Minenum K < Maxenum + 1; k++) {int cur = Math.max (dp[k-1][j-1], dp[i-k][j]);
                        if (cur <= min) {min = cur;
                    CANDS[J] = k;
            } Dp[i][j] = min + 1;
    } return dp[nlevel][kchess]; }//best solution, reverse thinking (Time complexity O (k*m)) public static int solution5 (int nlevel, int kchess) {if (Nlevel < 1 | | K
        Chess < 1) {return 0;
        int bstimes = log2n (nlevel) + 1;
        if (kchess >= bstimes) {return bstimes;
        } int[] dp = new Int[kchess];
        int res = 0;
            while (true) {res++;
            int previous = 0; for (int i = 0; i < Dp.lenGth
                i++) {int tmp = Dp[i];
                Dp[i] = Dp[i] + previous + 1;
                previous = tmp;
                if (Dp[i] >= nlevel) {return res;
        public static int log2n (int n) {int res =-1;
            while (n!= 0) {res++;
        n >>>= 1;
    return res;
        public static void Main (String[]args) {//system.out.println ("Hello");
        System.out.println (Solution1 (22, 2));
        System.out.println (Solution2 (22, 2));
        System.out.println (Solution3 (22, 2));
        System.out.println (Solution4 (22, 2));

        System.out.println (Solution5 (22, 2));

        System.out.println ("==============");
        System.out.println (Solution2 (105, 2));
        System.out.println (Solution3 (105, 2));
        System.out.println (Solution4 (105, 2));

        System.out.println (Solution5 (105, 2)); System.out.println ("==============");
        System.out.println (Solution2 (3000, 10));
        System.out.println (Solution3 (3000, 10));
        System.out.println (Solution4 (3000, 10));

        System.out.println (SOLUTION5 (3000, 10));

        System.out.println ("==============");
        System.out.println (Solution2 (6884, 5));
        System.out.println (Solution3 (6884, 5));
        System.out.println (Solution4 (6884, 5));

        System.out.println (Solution5 (6884, 5));

        System.out.println ("==============");
        System.out.println (Solution2 (6885, 5));
        System.out.println (Solution3 (6885, 5));
        System.out.println (Solution4 (6885, 5));

        System.out.println (Solution5 (6885, 5));

        System.out.println ("==============");
        int nlevel = 100000000;
        int kchess = 10;
        Long start = System.currenttimemillis ();
        System.out.println (Solution5 (Nlevel, kchess));
        Long end = System.currenttimemillis (); System.out.println ("Cost Time:" + (End-starT) + "MS"); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.