• 用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);
        }
    
    }
  • 相关阅读:
    RabbitMQ
    连接池,为什么要使用连接池?
    mac 安装arcanist
    感触
    UDP socket
    Servlet过滤器
    PL、SQL
    springmvc 文件上传实现(不是服务器的)
    注解spring
    excel工具类
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4004891.html
Copyright © 2020-2023  润新知