老师布置作业让写个不用recursive的Hanoi。终于写完了,就把它帖在这吧,尽管有点狗尾续貂的感觉。只是为了给自己点“成就感”。。。
</p>
<p>hanoi.whi<-function(n,from,via,to){<br />
pegs=list(1:n,NULL,NULL)<br />
hanoi.move<-function(n,from,via,to){<br />
if(n<2||n%%1!=0) stop("n should be an integer bigger than 1")<br />
if(n%%2==1){<br />
while(length(pegs[[from]])!=0||length(pegs[[via]])!=0){<br />
if(length(pegs[[from]])!=0&&length(pegs[[to]])!=0&&pegs[[from]][1]<pegs[[to]][1]||length(pegs[[to]])==0){<br />
cat("Move the disk of size",pegs[[from]][1],"from",LETTERS[from],"to",LETTERS[to],"\n")<br />
pegs[[to]]<<-c(pegs[[from]][1],pegs[[to]])<br />
pegs[[from]]<<-pegs[[from]][-1]<br />
} else if(length(pegs[[from]])!=0&&length(pegs[[to]])!=0&&pegs[[from]][1]>pegs[[to]][1]||length(pegs[[from]])==0){<br />
cat("Move the disk of size",pegs[[to]][1],"from",LETTERS[to],"to",LETTERS[from],"\n")<br />
pegs[[from]]<<-c(pegs[[to]][1],pegs[[from]])<br />
pegs[[to]]<<-pegs[[to]][-1]<br />
}<br />
if(length(pegs[[from]])==0&&length(pegs[[via]])==0) break;<br />
if(length(pegs[[from]])!=0&&length(pegs[[via]])!=0&&pegs[[from]][1]<pegs[[via]][1]||length(pegs[[via]])==0){<br />
cat("Move the disk of size",pegs[[from]][1],"from",LETTERS[from],"to",LETTERS[via],"\n")<br />
pegs[[via]]<<-c(pegs[[from]][1],pegs[[via]])<br />
pegs[[from]]<<-pegs[[from]][-1]<br />
} else if(length(pegs[[from]])!=0&&length(pegs[[via]])!=0&&pegs[[from]][1]>pegs[[via]][1]||length(pegs[[from]])==0){<br />
cat("Move the disk of size",pegs[[via]][1],"from",LETTERS[via],"to",LETTERS[from],"\n")<br />
pegs[[from]]<<-c(pegs[[via]][1],pegs[[from]])<br />
pegs[[via]]<<-pegs[[via]][-1]<br />
}<br />
if(length(pegs[[via]])!=0&&length(pegs[[to]])!=0&&pegs[[via]][1]<pegs[[to]][1]||length(pegs[[to]])==0){<br />
cat("Move the disk of size",pegs[[via]][1],"from",LETTERS[via],"to",LETTERS[to],"\n")<br />
pegs[[to]]<<-c(pegs[[via]][1],pegs[[to]])<br />
pegs[[via]]<<-pegs[[via]][-1]<br />
} else if(length(pegs[[via]])!=0&&length(pegs[[to]])!=0&&pegs[[via]][1]>pegs[[to]][1]||length(pegs[[via]])==0){<br />
cat("Move the disk of size",pegs[[to]][1],"from",LETTERS[to],"to",LETTERS[via],"\n")<br />
pegs[[via]]<<-c(pegs[[to]][1],pegs[[via]])<br />
pegs[[to]]<<-pegs[[to]][-1]<br />
}<br />
}<br />
} else {<br />
while(length(pegs[[from]])!=0||length(pegs[[via]])!=0){<br />
if(length(pegs[[from]])!=0&&length(pegs[[via]])!=0&&pegs[[from]][1]<pegs[[via]][1]||length(pegs[[via]])==0){<br />
cat("Move the disk of size",pegs[[from]][1],"from",LETTERS[from],"to",LETTERS[via],"\n")<br />
pegs[[via]]<<-c(pegs[[from]][1],pegs[[via]])<br />
pegs[[from]]<<-pegs[[from]][-1]<br />
} else if(length(pegs[[from]])!=0&&length(pegs[[via]])!=0&&pegs[[from]][1]>pegs[[via]][1]||length(pegs[[from]])==0){<br />
cat("Move the disk of size",pegs[[via]][1],"from",LETTERS[via],"to",LETTERS[from],"\n")<br />
pegs[[from]]<<-c(pegs[[via]][1],pegs[[from]])<br />
pegs[[via]]<<-pegs[[via]][-1]<br />
}<br />
if(length(pegs[[from]])!=0&&length(pegs[[to]])!=0&&pegs[[from]][1]<pegs[[to]][1]||length(pegs[[to]])==0){<br />
cat("Move the disk of size",pegs[[from]][1],"from",LETTERS[from],"to",LETTERS[to],"\n")<br />
pegs[[to]]<<-c(pegs[[from]][1],pegs[[to]])<br />
pegs[[from]]<<-pegs[[from]][-1]<br />
} else if(length(pegs[[from]])!=0&&length(pegs[[to]])!=0&&pegs[[from]][1]>pegs[[to]][1]||length(pegs[[from]])==0){<br />
cat("Move the disk of size",pegs[[to]][1],"from",LETTERS[to],"to",LETTERS[from],"\n")<br />
pegs[[from]]<<-c(pegs[[to]][1],pegs[[from]])<br />
pegs[[to]]<<-pegs[[to]][-1]<br />
}</p>
<p> if(length(pegs[[via]])!=0&&length(pegs[[to]])!=0&&pegs[[via]][1]<pegs[[to]][1]||length(pegs[[to]])==0){<br />
cat("Move the disk of size",pegs[[via]][1],"from",LETTERS[via],"to",LETTERS[to],"\n")<br />
pegs[[to]]<<-c(pegs[[via]][1],pegs[[to]])<br />
pegs[[via]]<<-pegs[[via]][-1]<br />
} else if(length(pegs[[via]])!=0&&length(pegs[[to]])!=0&&pegs[[via]][1]>pegs[[to]][1]||length(pegs[[via]])==0){<br />
cat("Move the disk of size",pegs[[to]][1],"from",LETTERS[to],"to",LETTERS[via],"\n")<br />
pegs[[via]]<<-c(pegs[[to]][1],pegs[[via]])<br />
pegs[[to]]<<-pegs[[to]][-1]<br />
}<br />
}<br />
}</p>
<p> }<br />
hanoi.move(n,from,via,to)<br />
}</p>
<p>hanoi.whi(3,1,2,3)<br />
</p>