首页 > 编程语言 > 详细

用java面向对象的思想实现的汉诺塔问题

时间:2014-10-03 13:25:44      阅读:248      评论:0      收藏:0      [点我收藏+]
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);
    }

}

 

用java面向对象的思想实现的汉诺塔问题

原文:http://www.cnblogs.com/ghgyj/p/4004891.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!