趣味Java_游戏_汉诺塔问题

趣味Java_002 游戏_题目:汉诺塔问题

汉诺塔问题(Hanoi Tower problem):有三根杆子A、B、C,A杆上串有上小下大若干碟子。每次移动一块碟子,在确保小碟子只能叠在大碟子上面的条件下,利用B过渡,请把所有碟子从A杆移到C杆上。:

  • 分析数据如图:

这里写图片描述

package com.jasonk.datatypeconversion.interest;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Stack;

public class DemoInterest_001 {
    /**
     * 【程序1】   题目:汉诺塔问题
     *  汉诺塔问题(Hanoi Tower problem):有三根杆子A、B、C,A杆上串有上小下大若干碟子。每次移动一块碟子,在确保小碟子只能叠在大碟子上面的条件下,利用B过渡,请把所有碟子从A杆移到C杆上。
     * @param args
     */
    public static void main(String[] args) {
        coreStr(2);
    }
    /**
     * 核心排序算法
     * @param start     开始积木位置
     * @param middle    中转积木位置
     * @param end       最终积木位置
     */
    /**
     * 主子A
     */
    public static final String A = "A";
    /**
     * 主子B
     */
    public static final String B = "B";
    /**
     * 主子C
     */
    public static final String C = "C";
    public static final String A0 = "A";// A
    public static final String B0 = "B";// B
    public static final String C0 = "C";// C
    public static final String token = "➺";// 移动指向标识
    public static final String f0 = " ";// 空格    // 打印间隔标识
    public static final String f1 = "|";// |    // 柱子代表标识
    public static final String f2 = "-";// -    // 最下层基台标识
    public static final String f3 = "=";// =    //积木代表标识

    /*public static final String A0 = "?";// A
    public static final String B0 = "?";// B
    public static final String C0 = "?";// C
    public static final String token = "➺";// 移动指向标识
    public static final String f0 = "?";// 空格    // 打印间隔标识
    public static final String f1 = "?";// |    // 柱子代表标识
    public static final String f2 = "?";// -    // 最下层基台标识
    public static final String f3 = "❎";// =    //积木代表标识*/

    /**
     * 记录移动步骤
     */
    public static LinkedList<String> p = new LinkedList<String>(); 
    /**
     * 记录积木在主子上的情况
     */
    public static Map<String, Stack<Integer>> columns = new HashMap<String, Stack<Integer>>();
    /**
     * 核心
     * @param start     开始积木位置
     * @param middle    中转积木位置
     * @param end       最终积木位置
     * @param num       积木数
     * @param count     距离中轴下方数值
     */
    public static void core(String start,String middle,String end,int num,int count){
        // 计算中轴数据位置
        int con = (((int)Math.pow(2, num)-1)/2);
        // 中轴数据插入
        p.set(con+count, start+token+end);
        // 判断是否深入添加步数
        if((((int)Math.pow(2, num)-1)/2)>0){
            // 递归添加上行数据
            core( start, end, middle, num-1, count);
            // 递归添加下行数据
            core( middle, start, end, num-1, con+count+1);
        }
    }
    /**
     * 前期数据准备
     * @param num
     */
    public static void coreStr(int num){
        // 初始化开始柱子的情况
        Stack<Integer> integers = new Stack<Integer>();
        for(int i=num;i>0;i--){
            integers.add(i);
        }
        // 初始化当前积木数所需的全部步骤数
        for(int i=0;i<(Math.pow(2, num)-1);i++){
            p.add(null);
        }
        // 设置各个柱子的积木情况
        columns.put("A", integers);
        columns.put("B", new Stack<Integer>());
        columns.put("C", new Stack<Integer>());
        core( A, B, C, num, 0);
        // 打印显示初始积木状况
        System.out.println("初始积木状态:");
        print(null, null, columns, num);
        // 打印之后每移动一步后各个柱子积木情况
        int m = 1;
        for(String i : p){// 循环移动步骤
            System.out.println("第"+m+"步:"+i);m++;
            print(i.split(token)[0], i.split(token)[1], columns, num);
        }
    }
    /**
     * 按照步骤改变主子上积木的位置
     * @param start
     * @param end
     * @param columns
     */
    public static void move(String start,String end, Map<String, Stack<Integer>> columns){
        if(columns!=null && columns.get(start)!=null && columns.get(end)!=null){
            columns.get(end).push(columns.get(start).lastElement());
            columns.get(start).remove((columns.get(start).size()-1));
        }
    }
    /**
     * 按照步骤移动积木并打印显示
     * @param start
     * @param end
     * @param columns
     * @param num
     */
    public static void print(String start,String end, Map<String, Stack<Integer>> columns, int num){
        if(start!=null && end !=null){          
            move(start, end, columns);
        }
        if(columns!=null && columns.size()>0){
            for(int i=2+num;i>0;i--){
                StringBuffer str = new StringBuffer();
                if(i==1){
                    str.append(f0);
                    str.append(getStr(f2, num+1));
                    str.append(A0);
                    str.append(getStr(f2, num+1));
                    str.append(f0);
                    str.append(getStr(f2, num+1));
                    str.append(B0);
                    str.append(getStr(f2, num+1));
                    str.append(f0);
                    str.append(getStr(f2, num+1));
                    str.append(C0);
                    str.append(getStr(f2, num+1));
                }else if(i==2+num){
                    for(byte m=3;m>0;m--){
                        str.append(f0);
                        str.append(getStr(f0, num+1));
                        str.append(f1);
                        str.append(getStr(f0, num+1));
                    }
                }else{
                    Stack<Integer> col = columns.get(A);
                    str.append(f0);
                    int m = 0;
                    if(col.size()>=(i-1)){
                        m = col.get((i-2));
                    }
                    str.append(getStr(f0, num+1-m));
                    str.append(getStr(f3, m));
                    str.append(f1);
                    str.append(getStr(f3, m));
                    str.append(getStr(f0, num+1-m));

                    col = columns.get(B);
                    str.append(f0);
                    m = 0;
                    if(col.size()>=(i-1)){
                        m = col.get((i-2));
                    }
                    str.append(getStr(f0, num+1-m));
                    str.append(getStr(f3, m));
                    str.append(f1);
                    str.append(getStr(f3, m));
                    str.append(getStr(f0, num+1-m));

                    col = columns.get(C);
                    str.append(f0);
                    m = 0;
                    if(col.size()>=(i-1)){
                        m = col.get((i-2));
                    }
                    str.append(getStr(f0, num+1-m));
                    str.append(getStr(f3, m));
                    str.append(f1);
                    str.append(getStr(f3, m));
                    str.append(getStr(f0, num+1-m));
                }
                System.out.println(str.toString());
            }
        }
    }
    /**
     * 拼接相同字符
     * @param str
     * @param num
     * @return
     */
    public static String getStr(String str,int num){
        StringBuffer s = new StringBuffer();
        for(int i=num;i>0;i--){
            s.append(str);
        }
        return s.toString();
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值