狼羊过河 回溯实现JAVA

狼羊过河 回溯实现JAVA

贴上代码

判断路径是否走过

    static class Stu{
        int sheep;
        int wolf;
        int sheep0;
        int wolf0;

        public Stu(int sheep, int wolf, int sheep0, int wolf0) {
            this.sheep = sheep;
            this.wolf = wolf;
            this.sheep0 = sheep0;
            this.wolf0 = wolf0;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Stu stu = (Stu) o;
            return sheep == stu.sheep && wolf == stu.wolf && sheep0 == stu.sheep0 && wolf0 == stu.wolf0;
        }

        @Override
        public int hashCode() {
            return Objects.hash(sheep, wolf, sheep0, wolf0);
        }
    }

完整代码

static class Stu{
        int sheep;
        int wolf;
        int sheep0;
        int wolf0;

        public Stu(int sheep, int wolf, int sheep0, int wolf0) {
            this.sheep = sheep;
            this.wolf = wolf;
            this.sheep0 = sheep0;
            this.wolf0 = wolf0;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Stu stu = (Stu) o;
            return sheep == stu.sheep && wolf == stu.wolf && sheep0 == stu.sheep0 && wolf0 == stu.wolf0;
        }

        @Override
        public int hashCode() {
            return Objects.hash(sheep, wolf, sheep0, wolf0);
        }
    }

    static int ans = Integer.MAX_VALUE;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int sheep = in.nextInt();
        int wolf = in.nextInt();
        int x = in.nextInt();

        transport(new StringBuffer(), sheep, wolf, 0, 0, x, 0, new HashSet<Stu>());
    }

    private static int transport(StringBuffer sb, int sheep0, int wolf0, int sheep1, int wolf1, int x,
                                  int temp, HashSet<Stu> set) {
        if (temp > ans) return 0;
        if (sheep0 == 0 && wolf0 == 0) {
            if (ans > temp) ans = temp;
            System.out.println(sb);
            System.out.println("次数:"+temp);
            return ans;
        }
        if (x >= sheep0 + wolf0) {
            temp++;
            if (ans > temp) ans = temp;
            sb.append("运羊:"+sheep0+",运狼:"+wolf0+"\n");
            System.out.println(sb);
            System.out.println("次数:"+temp);
            return ++ans;
        }

        Stu stu;
        for (int i = 0; i <= sheep0 && i <= x; ++i) {
            for (int j = 0; j <= wolf0 && i+j <= x; ++j) {
                if (i == 0 && j == 0) continue;
                stu = new Stu(i, j, sheep0-i, wolf0-j);
                if (!set.contains(stu)) {
                    set.add(stu);
                    StringBuffer sb1 = new StringBuffer(sb);
                    // 左岸 右岸    i,j 床上羊狼
                    if (conditionTrue(sheep0-i, wolf0-j)
                            && conditionTrue(sheep1+i, wolf1+j)) {
                        transport(sb.append("运羊:"+i+",运狼:"+j+"\n"), sheep0-i, wolf0-j, sheep1+i,
                                wolf1+j, x, temp+1, set);
                        sb = sb1;
                        set.remove(stu);
                    }
                }

            }
        }
        return 0;
    }


    private static boolean conditionTrue(int sheep, int wolf) {
        if (sheep > wolf || sheep == 0) {
            return true;
        }
        return false;
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值