zoukankan      html  css  js  c++  java
  • 用java面向对象的思想实现的汉诺塔问题

    package hanoi.com;
    
    public class Disc {
        private String name;
        private int level;
        public Disc(){
            name = "disc";
            level = 0;
        }
        public Disc(String name, int level){
            this.name = name;
            this.level = level;
        }
        
        public String getName() {
            return name;
        }
        public int getLevel() {
            return level;
        }
        @Override
        public String toString() {
            return "name: " + this.name + ";level: " + this.level;
        }
    }
    package hanoi.com;
    import java.util.LinkedList;
    import java.util.List;
    
    public class Post {
        //用来装Disc
        private List<Disc> lists = new LinkedList<Disc>();
        private String name;public String getName() {
            return name;
        }
    
        public Post(String name) {
            this.name = name;
        }
    
        public void add(Disc disc) {
            if(disc == null) return;
            this.lists.add(0, disc);
        }
    
        public void add(List<Disc> discs) {
            if (discs == null || discs.size() < 1) {
                return;
            }
            lists.addAll(0, discs);
        }
        //当前柱子中的盘子借助middlePost移到到targetPost
        public void move(Post middlePost, Post targetPost,PostMove postMove){
            this.move(this.lists.size(), middlePost, targetPost, postMove);
        }
        private void move(int total, Post post1, Post post2,PostMove postMove) {
            if(total <= 0) return;
            this.move(total - 1, post2, post1,postMove);
            
            
            Disc disc = this.lists.remove(0);
            if(postMove != null){
                postMove.action(this, post1,post2, disc);
            }
            post2.add(disc);
            
            post1.move(total -1 ,this, post2,postMove);
        }
    
        @Override
        public String toString() {
            return "Post [lists=" + lists + ", name=" + name + "]";
        }
    }
    package hanoi.com;
    
    
    public interface PostMove {
        public void action(Post scrPost,Post middlePost, Post targetPost, Disc disc);
    }
    package hanoi.com.test;
    
    import hanoi.com.Disc;
    import hanoi.com.Post;
    import hanoi.com.PostMove;
    
    public class Main {
    
        public static void main(String[] args) {
            Post post1 = new Post("A");
            Post post2 = new Post("B");
            Post post3 = new Post("C");
    
            post1.add(new Disc("3", 2));
            post1.add(new Disc("2", 1));
            post1.add(new Disc("1", 0));
    
            System.out.println(post1);
            System.out.println(post2);
            System.out.println(post3);
    
            
            post1.move(post2, post3, new PostMove() {
    
                @Override
                public void action(Post srcPost, Post middlePost,Post targetPost, Disc disc) {
                    System.out.println(disc + ":从"+ srcPost.getName()  + "通过" + middlePost.getName() + "到达" + targetPost.getName());
                }
            });
            System.out.println(post1);
            System.out.println(post2);
            System.out.println(post3);
        }
    
    }
  • 相关阅读:
    eclipse 粘贴字符串自动添加转义符
    原创 导出微信收藏到电脑
    解决 eclipse中properties文件编码问题
    jetty 内嵌服务
    解决log4j:WARN No appenders could be found for logger
    解决JettyMavenPlugin: Failed to load class "org.slf4j.impl.StaticLoggerBinder"
    读取properties文件方式
    解决ssh远程登陆linux显示-bash-4.1$
    解决weblogic.net.http.SOAPHttpsURLConnection incompatible with javax.net.ssl.HttpsURLConnection
    解决java.io.IOException: HTTPS hostname wrong: should be
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4004891.html
Copyright © 2011-2022 走看看