码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-10-03 14:42:04      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   java   sp   div   

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面向对象的思想实现的汉诺塔问题

标签:style   blog   color   io   os   ar   java   sp   div   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!