用回溯法求解打靶问题

打靶打10次,打中90环的可能性有多少?


用回溯法实现:

控制台输出:

递归实现:打10次90环的可能性:92378种
迭代实现:打10次90环的可能性:92378种


package AlgorithmTest;

import java.util.Stack;

/**
 * Created by dell on 2016/12/18.
 */
public class ShootingProblem {
    public static void main(String[] args) {
        ShootingProblemTree shootingProblemTree = new ShootingProblemTree(10, 90);//打10次要求90环
        ShootingProblemTree shootingProblemTree1 = new ShootingProblemTree(10, 90);
        System.out.println("递归实现:打10次90环的可能性:" + shootingProblemTree.getSum() + "种");
        System.out.println("迭代实现:打10次90环的可能性:" + shootingProblemTree1.getSumIteration() + "种");
    }

    //每一次有0换到10换,11种可能性
    private static class ShootingProblemTree{
        private int count;
        private int circles;
        private int sum;

        public ShootingProblemTree(int count, int circles) {
            this.count = count;
            this.circles = circles;
        }

        private int getSum(){
            solve(0, 0, -1);
            return this.sum;
        }
        private int getSumIteration(){
            solveIteration(0, 0, -1);
            return this.sum;
        }
        private static class Three{
            private int node;
            private int sum;
            private int nodes;

            public Three(int node, int sum, int nodes) {
                this.node = node;
                this.sum = sum;
                this.nodes = nodes;
            }
        }
        private void solveIteration(int node, int sum, int nodes){
            Stack<Three> threes = new Stack<Three>();
            threes.push(new Three(node, sum, nodes));
            while (!threes.isEmpty()){
                Three three = threes.pop();
                node = three.node;
                sum = three.sum;
                nodes = three.nodes;
                if (sum + node <= this.circles && sum + node + (this.count - nodes - 1)*10 >= this.circles){
                    if (nodes + 1 < this.count){
                        for (int i = 10; i >= 0; i--){
                            threes.push(new Three(i, sum + node, nodes + 1));
                        }
                    }else{
                        if (sum + node == this.circles){
                            this.sum++;
                        }
                    }

                }
            }

        }
        private void solve(int node, int sum, int nodes){
            if (sum + node <= this.circles && sum + node + (this.count - nodes - 1)*10 >= 90 ){
                if (nodes + 1 < this.count){
                    for (int i = 0; i < 11; i++){
                        solve(i, sum + node, nodes + 1);
                    }
                }else{
                    if (sum + node == this.circles){
                        this.sum++;
                    }
                }

            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值