• 排列小球


    愚蠢的做法如下(我也差不多是这样做的。。。)

    public class Random23 {
    
    
    
        static int countWays(int p, int q, int r, int last)
        {
            if (p < 0 || q < 0 || r < 0)
                return 0;
            if (p == 1 && q == 0 && r == 0 && last == 0)
                return 1;
    
            // Same case as above for 'q' and 'r'
            if (p == 0 && q == 1 && r == 0 && last == 1)
                return 1;
            if (p == 0 && q == 0 && r == 1 && last == 2)
                return 1;
            if (last == 0)
                return countWays(p - 1, q, r, 1) +
                        countWays(p - 1, q, r, 2);
            if (last == 1)
                return countWays(p, q - 1, r, 0) +
                        countWays(p, q - 1, r, 2);
    
            if (last == 2)
                return countWays(p, q, r - 1, 0) +
                        countWays(p, q, r - 1, 1);
    
            return 0;
        }
    
        // Returns count of required arrangements
        static int countUtil(int p, int q, int r) {
            return countWays(p, q, r, 0) + // Last required balls is type P
                    countWays(p, q, r, 1) + // Last required balls is type Q
                    countWays(p, q, r, 2); // Last required balls is type R
        }
    
    
        public static void main(String[] args)
        {
            int p = 11, q = 11, r = 11;
            System.out.print(countUtil(p, q, r));
        }
    
    
    
    }

      其实想下做了很多重复的计算,这儿其实完全可以想到dp,保存下之前的计算结果,这样每个x,y,z组合就可以只执行一次。按照这个思维的话做法在原基础上改进一下

    public class Random22 {
    
        public static void main(String[] args) {
            Random22 ra = new Random22();
            Scanner in=new Scanner(System.in);
            while(in.hasNext()){
                String[] temp=in.nextLine().split(" ");
                int x = Integer.parseInt(temp[0]);
                int y = Integer.parseInt(temp[1]);
                int z = Integer.parseInt(temp[2]);
                long result = ra.getAll(x, y, z);
                System.out.println(result);
            }
        }
    
        //long result;
        long[][][][] dp;
        public  long getAll(int x,int y,int z){
            long result = 0;
            dp = new long[x+1][y+1][z+1][4];
            result+= dsf(1, x - 1, y, z);
            result+= dsf(2, x, y - 1, z);
            result+=dsf(3,x,y,z-1);
            return result;
        }
    
        public long dsf(int index,int x,int y,int z){
            if (x == 0 && y == 0 && z == 0){
                dp[0][0][0][index] = 1;
                return 1;
            }
            long s = 0;
            if (index == 1){
                if (y >0){
                    long t = dp[x][y-1][z][2];
                    if ( t!= 0){
                        s+=t;
                    }else {
                        s+=dsf(2,x,y-1,z);
                    }
                }
                if (z > 0){
                    long t = dp[x][y][z-1][3];
                    if ( t!= 0){
                        s+=t;
                    }else {
                        s+=dsf(3,x,y,z-1);
                    }
    
                }
            }else if (index == 2){
                if (x >0){
                    long t = dp[x-1][y][z][1];
                    if ( t!= 0){
                        s+=t;
                    }else {
                        s+=dsf(1,x-1,y,z);
                    }
    
                }
                if (z > 0){
                    long t = dp[x][y][z-1][3];
                    if ( t!= 0){
                        s+=t;
                    }else {
                        s+=dsf(3,x,y,z-1);
                    }
    
                }
            }else {
                if (x >0){
                    long t = dp[x-1][y][z][1];
                    if ( t!= 0){
                        s+=t;
                    }else {
                        s+=dsf(1,x-1,y,z);
                    }
    
                }
                if (y >0){
                    long t = dp[x][y-1][z][2];
                    if ( t!= 0){
                        s+=t;
                    }else {
                        s+=dsf(2,x,y-1,z);
                    }
                }
            }
            dp[x][y][z][index] = s;
            return s;
        }
    }
  • 相关阅读:
    java基础学习总结——哈希编码
    java基础学习总结——static关键字
    java基础学习总结——方法的重载(overload)
    java基础学习总结——流
    java基础学习总结——线程(二)
    java基础学习总结——线程(一)
    阻塞队列---ArrayBlockingQueue,LinkedBlockingQueue,DelayQueue源码分析
    hashCode 一致性hash 算法
    byte以及UTF-8的转码规则
    vue cli 解决跨域 线上 nginx 反向代理配置
  • 原文地址:https://www.cnblogs.com/hetutu-5238/p/14582591.html
Copyright © 2020-2023  润新知