汉诺塔问题演示

2008-10-04 13:51:00 | 【
点击开始按钮开始演示

层数
延时

计算机使用最快的移动方法演示汉诺塔问题。你可以自己调整层数和移动速度

查看全部代码 View Code

第一步:
在<head>区添加以下内容

<SCRIPT LANGUAGE="JavaScript">

        /*****************************************************
         *  Share JavaScript (http://www.ntpdmu.com.cn)
         * 使用此脚本程序,请保留此声明
         * 获取此脚本以及更多的JavaScript程序,请访问 http://www.ntpdmu.com.cn
         ******************************************************/
    
<!-- Begin
var MAXHEIGHT = 8;
var ndisk;
var timer = null;
var mvfrom = new Array(255);
var mvto = new Array(255);
var mv, imv;
var tower = new Array(3);
var h = new Array(3);
var spc = "        ";
var dname = new Array(
"|",
"111",
"22222",
"3333333",
"444444444",
"55555555555",
"6666666666666",
"777777777777777",
"88888888888888888");

function init() {
if (!timer) {
s = document.formHanoi.disk.options[document.formHanoi.disk.selectedIndex].value;
if (s == "random") {
now = new Date();
ndisk = parseInt(now.getTime() / 1000) % 8 + 1;
delete now;
}
else ndisk = s;
mv = 0;
hanoi(0, 2, 1, ndisk);
for (i = 0; i < 3; i++)
tower[i] = new Array(MAXHEIGHT);
for (i = 0; i < ndisk; i++)
tower[0][i] = ndisk - i;
h[0] = ndisk;
h[1] = h[2] = 0;
imv = 0;
document.formHanoi.display.value = gentower();
timer = window.setTimeout("gennexttower()", document.formHanoi.delay.options[document.formHanoi.delay.selectedIndex].value);
   }
}
function stop() {
if (timer) {
window.clearTimeout(timer);
timer = null;
   }
}
function hanoi(from,to,buf,nmv) {
if (nmv > 1) {
hanoi(from, buf, to, nmv - 1);
mvfrom[mv] = from;
mvto[mv++] = to;
hanoi(buf, to, from, nmv - 1);
}
else {
mvfrom[mv] = from;
mvto[mv++] = to;
   }
}
function gennexttower() {
tower[mvto[imv]][h[mvto[imv]]++] = tower[mvfrom[imv]][--h[mvfrom[imv]]];
document.formHanoi.display.value = gentower();
if (++imv < mv)
timer = window.setTimeout("gennexttower()", document.formHanoi.delay.options[document.formHanoi.delay.selectedIndex].value);
else {
for (i = 0; i < 3; i++)
delete tower[i];
timer = null;
    }
}
function gentower() {
s = " \n";
for (i = MAXHEIGHT - 1; i >= 0; i--) {
for (j = 0; j < tower.length; j++) {
len = i < h[j] ? tower[j][i] : 0;
width = MAXHEIGHT - len;
s += " " + spc.substring(0, width) + dname[len] + spc.substring(0, width);
}
s += "\n";
}
return s+"=======================================================";
}
//  End -->
</script>


第二步:
在<body>区添加以下内容
<center>
<form name=formHanoi>
<textarea name=display rows=10 cols=56 /><br>
<table border=0 cellspacing=10 cellpadding=0>
<tr>
<td>层数</td>
<td><select name=disk>
<option selected value=random>随机
<option value=1>1
<option value=2>2
<option value=3>3
<option value=4>4
<option value=5>5
<option value=6>6
<option value=7>7
<option value=8>8
</select></td>
</tr>
<tr>
<td>延时</td>
<td><select name=delay>
<option value=100>100 毫秒
<option value=200>200 毫秒
<option value=300>300 毫秒
<option value=400>400 毫秒
<option selected value=500>500 毫秒
<option value=600>600 毫秒
<option value=700>700 毫秒
<option value=800>800 毫秒
<option value=900>900 毫秒
<option value=1000>1000 毫秒
</select></td>
</tr>
</table>
<input type=button value="开始" onClick="init();">
<input type=button value="停止" onClick="stop();">
</form>
</center>

下载"汉诺塔问题演示"

  • 本地下载
  • 本地下载2


推荐资源

大赢家足球比分直播