标题:如何在java平台下采用递规算法做汉诺塔演示程序
只看楼主
hengshan
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-4-15
 问题点数:0 回复次数:16 
如何在java平台下采用递规算法做汉诺塔演示程序
我想用java做汉诺塔演示程序,可是要用递规算法实现,样让才能实现每隔一秒移动一个盘子?
搜索更多相关主题的帖子: 汉诺塔 规算法 演示程序 java 平台 
2005-11-06 13:53
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
得分:0 
有图象的吗?

淘宝杜琨
2005-11-06 13:58
hengshan
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-4-15
得分:0 

是有图像的,就像动画一样让他动起来,因为是递规算法,所以我不知道怎么控制,好像在线程里面不好控制

2005-11-06 14:13
惊蛰
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2005-10-11
得分:0 
可以把源程序复制上来,与大家共勉吗。
2005-11-06 16:36
hengshan
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2005-4-15
得分:0 

可以啊,只是我现在有这个想法,不知道怎么实现,请高人指点,希望有兴趣的同仁共同探讨和研究。因为我现在在学习算法分析与设计,我想做好多经典算法的演示程序,也希望有兴趣的同仁共同探讨和研究。

2005-11-07 07:07
九层楼
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2005-11-9
得分:0 
讨论算法实现,是否把算法的伪代码实现贴上来?

大家很多都不熟悉算法,贴上实现,应该会对大家都有帮助
2005-11-09 22:24
liting
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2005-11-12
得分:0 

我以前做过一个类似的程序,既可以手动移动塔,也可以自动移塔,移动时间也可以调节。

2005-11-13 08:18
zhxycz
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-5-25
得分:0 
这是一个自动演示汉诺塔的例子
u7Lh1Ztd.rar (11.1 KB) 如何在java平台下采用递规算法做汉诺塔演示程序



2006-05-25 11:00
★王者至尊★
Rank: 1
等 级:新手上路
帖 子:528
专家分:0
注 册:2006-3-28
得分:0 
不错 这个写的相当好

------Java 爱好者,论坛小混混,学习中------
2006-05-25 14:34
afc1618
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2006-4-22
得分:0 

我也传上去源代码,望大家指点
package hannuota;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Hanoi
extends JFrame
implements ActionListener {
private Thread auto;
private int n = 4;
private int[] a = new int[3];
private int[][] x;
private int[] y;
private int[][] w;
private int h = 5;
private void initial() {
a[0] = n;
a[1] = 0;
a[2] = 0;
x = new int[3][n];
y = new int[n];
w = new int[3][n];
for (int i = 0; i < n; i++) {
x[0][i] = 75 - 5 * (n - i);
y[i] = 185 - i * h;
w[0][i] = 10 + 10 * (n - i);
}
}

private static int FROM;
private static int TO;

private int line_1_x = 80, line_2_x = 190, line_3_x = 300, line_y1 = 10,
line_y2 = 190;
private int fk_height = 5;

private boolean[] b = new boolean[3];
private boolean started;
private boolean stopAuto;

private Thread colorControler = null;

private Time clock = new Time();

private JTextField counter = new JTextField(5);
private int c;
private JPanel0 jp = new JPanel0();
private JButton[] jb = {
null, null, null};
private Box box = Box.createHorizontalBox();

private JMenuBar jmb = new JMenuBar();
private JMenu jm = new JMenu("文件(F)");
private JMenuItem jmi[] = {
null, null, null, null, null, null};

JMenuItem stop = new JMenuItem("stop");
JMenuItem goOn = new JMenuItem("goOn");

public Hanoi() {
setSize(500, 300);
setTitle("汉诺塔");
addWindowListener(new WindowDestroyer());
Container cp = getContentPane();
cp.setLayout(new BorderLayout());

auto = new Auto();

JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(new JLabel("时间"));
panel.add(clock);
panel.add(new JLabel("次数"));
panel.add(counter);
initial();

jp.add(panel, BorderLayout.NORTH);
counter.setEditable(false);
counter.setBackground(Color.white);
counter.setText("0");
jp.setBackground(Color.blue);
cp.add(jp, BorderLayout.CENTER);

jb[0] = new JButton("0");
jb[0].addActionListener(this);
box.add(Box.createHorizontalStrut(60));
box.add(jb[0]);

jb[1] = new JButton("1");
jb[1].addActionListener(this);
box.add(Box.createHorizontalStrut(70));
box.add(jb[1]);

jb[2] = new JButton("2");
jb[2].addActionListener(this);
box.add(Box.createHorizontalStrut(70));
box.add(jb[2]);

jmi[0] = new JMenuItem("开始");
jmi[0].addActionListener(this);
jm.add(jmi[0]);

stop.addActionListener(this);
//stop.setEnabled(false);
jm.add(stop);
goOn.addActionListener(this);
//stop.setEnabled(false);
jm.add(goOn);

jmi[1] = new JMenuItem("自动");
jmi[1].addActionListener(this);
jm.add(jmi[1]);
jmi[2] = new JMenuItem("退步");
jmi[2].addActionListener(this);
jm.add(jmi[2]);
jmi[3] = new JMenuItem("重新开始");
jmi[3].addActionListener(this);
jm.add(jmi[3]);
jmi[4] = new JMenuItem("退出");
jmi[4].addActionListener(this);
jm.add(jmi[4]);
jm.setMnemonic('F');
jmb.add(jm);

jmi[5] = new JMenu("级别(L)");
jmi[5].setMnemonic('L');
JMenuItem mi;
mi = new JMenuItem("3");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("4");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("5");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("6");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("7");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("8");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("9");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("10");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("11");
mi.addActionListener(this);
jmi[5].add(mi);
mi = new JMenuItem("12");
mi.addActionListener(this);
jmi[5].add(mi);
jmb.add(jmi[5]);

setJMenuBar(jmb);

cp.add(box, BorderLayout.SOUTH);
//colorControler = new ColorControler();
//colorControler.start();
}

class Auto
extends Thread {
public void run() {
m(n, 0, 2, 1);
}

private void m(int N, int from, int to, int spare) {
if (N == 1) {
synchronized (auto) {
try {
auto.sleep(500);
if (stopAuto) {
wait();
}
}
catch (Exception e) {
System.out.println("m() wrong");
}
move1(from, to);
}
}
else {
m(N - 1, from, spare, to);
synchronized (auto) {
try {
auto.sleep(500);
if (stopAuto) {
wait();
}
}
catch (Exception e) {
System.out.println("m() wrong");
}
move1(from, to);
}
m(N - 1, spare, to, from);
}
}
}

/*private class ColorControler
extends Thread {
public void run() {
while (true) {
if (a[2] == n) {
clock.stop();
}
for (int i = 0; i < 3; i++) {
if (b[i]) {
jb[i].setBackground(Color.red);
}
else {
jb[i].setBackground(Color.lightGray);
}
}
}
}
}*/

private class JPanel0
extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(line_1_x, line_y1, line_1_x, line_y2);
g.drawLine(line_2_x, line_y1, line_2_x, line_y2);
g.drawLine(line_3_x, line_y1, line_3_x, line_y2);
g.drawLine(0, line_y2, line_3_x + 100, line_y2);

g.setColor(Color.yellow);

for (int j = 0; j < 3; j++) {
for (int i = 0; i < a[j]; i++) {
g.fillRect(x[j][i], y[i], w[j][i], h);
}
}
}
}

public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
if (ac.equals("开始")) {
stop.setEnabled(true);
started = true;
clock.start();
}
else if (ac.equals("stop")) {
stopAuto = true;
//clock.stop();
}
else if (ac.equals("goOn")) {
stopAuto = false;
synchronized (auto) {
try {
auto.notify();
}
catch (Exception ef) {}
}
synchronized (clock.time) {
try {
clock.time.notify();
}
catch (Exception ef) {}
}
}

else if (ac.equals("3") || ac.equals("4") ||
ac.equals("5") || ac.equals("6") || ac.equals("7") ||
ac.equals("8") || ac.equals("9") || ac.equals("10") ||
ac.equals("11") || ac.equals("12")) {
n = Integer.parseInt(ac);
initial();
jp.repaint();
/*Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int height = screenSize.height;
int width = screenSize.width;
setLocation( (height - 50) / 2, (width - 30) / 2);*/
}
else if (ac.equals("自动")) {
try {
clock.start();
auto.start();
}
catch (Exception ef) {
System.out.println("m wrong");
}
}
else if (ac.equals("退步")) {
move1(TO, FROM);
c -= 2;
counter.setText("" + c);
}
else if (ac.equals("重新开始")) {
dispose();
(new Hanoi()).setVisible(true);
}
else if (ac.equals("退出")) {
System.exit(0);
}

else if (started && ac.equals("0")) {
b[0] = !b[0];
changeColor();
//jb[0].setBackground(Color.red);
//jb[0].setBackground(Color.lightGray);
if (b[1]) {
if (a[0] == 0 || w[0][a[0] - 1] > w[1][a[1] - 1]) {
move1(1, 0);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
}
else if (b[2]) {
if (a[0] == 0 || w[0][a[0] - 1] > w[2][a[2] - 1]) {
move1(2, 0);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
}
}
else if (started && ac.equals("1")) {
b[1] = !b[1];
changeColor();
if (b[0]) {
if (a[1] == 0 || w[1][a[1] - 1] > w[0][a[0] - 1]) {
move1(0, 1);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
}
else if (b[2]) {
if (a[1] == 0 || w[1][a[1] - 1] > w[2][a[2] - 1]) {
move1(2, 1);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
}
}
else if (started && ac.equals("2")) {
b[2] = !b[2];
changeColor();
if (b[0]) {
if (a[2] == 0 || w[2][a[2] - 1] > w[0][a[0] - 1]) {
move1(0, 2);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
changeColor();
}

else if (b[1]) {
if (a[2] == 0 || w[2][a[2] - 1] > w[1][a[1] - 1]) {
move1(1, 2);
}
else {
System.out.println("can't do that");
}
b[0] = b[1] = b[2] = false;
changeColor();
}
}
}

private void changeColor() {
for (int i = 0; i < 3; i++) {
if (!b[i]) {
jb[i].setBackground(Color.lightGray);
}
//if( b.getBackground().equals(Color.lightGray)){
else {
jb[i].setBackground(Color.red);
}
}
}

private void move1(int from, int to) {
FROM = from;
TO = to;
c++;
counter.setText("" + c);
x[to][a[to]] = x[from][a[from] - 1] + (to - from) * 110;
w[to][a[to]] = w[from][a[from] - 1];
a[from]--;
a[to]++;
jp.repaint();
}

private class Time
extends JTextField
implements Runnable {
Thread time = null;
private int i;
public Time() {
super(5);
setEditable(false);
setBackground(Color.white);
setText(0 + "");
}

public void start() {
if (time == null) {
time = new Thread(this);
time.start();
}
}

public void stop() {
time = null;
}

public void run() {
while (time != null) {
setText(i + "");
synchronized (time) {

try {
time.sleep(1000);
time.wait();
}
catch (InterruptedException e) {
setText("clock broken");
}
}
i++;
}
}
}

public static void main(String[] args) {
Hanoi hanoi1 = new Hanoi();
hanoi1.setVisible(true);
}
}

class WindowDestroyer
extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}


望众高手不吝赐教!


2006-05-25 18:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-32834-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.096824 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved