Java 河内塔问题解决方案
我正在调试河内塔问题的一些解决方案,总是发现一条令人困惑的规则。我认为有一个限制规定“一个圆盘从一根杆的顶部滑到下一根杆上” 我的问题是,如果第45行起作用,这意味着“缓冲”棒紧挨着当前棒,“目标”棒不应该紧挨着当前棒。这意味着第46行是不正确的,因为我们移动到一个棒的位置,而不是当前棒的位置 但这似乎是唯一的解决办法?如果有人能澄清这一困惑,那就太好了 以下是我正在调试的代码:Java 河内塔问题解决方案,java,algorithm,recursion,Java,Algorithm,Recursion,我正在调试河内塔问题的一些解决方案,总是发现一条令人困惑的规则。我认为有一个限制规定“一个圆盘从一根杆的顶部滑到下一根杆上” 我的问题是,如果第45行起作用,这意味着“缓冲”棒紧挨着当前棒,“目标”棒不应该紧挨着当前棒。这意味着第46行是不正确的,因为我们移动到一个棒的位置,而不是当前棒的位置 但这似乎是唯一的解决办法?如果有人能澄清这一困惑,那就太好了 以下是我正在调试的代码: public static void main(String[] args) { int n = 5;
public static void main(String[] args) {
int n = 5;
Tower[] towers = new Tower[n];
for (int i = 0; i < 3; i++) towers[i] = new Tower(i);
for (int i = n - 1; i >= 0; i--) towers[0].add(i);
towers[0].moveDisks(n, towers[2], towers[1]);
}
public class Tower {
private Stack < Integer > disks;
private int index;
public Tower(int i) {
disks = new Stack < Integer > ();
index = i;
}
public int index() {
return index;
}
public void add(int d) {
if (!disks.isEmpty() && disks.peek() <= d) {
System.out.println(“Error placing disk” + d);
} else {
disks.push(d);
}
}
public void moveTopTo(Tower t) {
int top = disks.pop();
t.add(top);
System.out.println(“Move disk” + top + “from” + index() + “to” + t.index());
}
public void print() {
System.out.println(“Contents of Tower“ + index());
for (int i = disks.size() - 1; i >= 0; i--) {
System.out.println(““ + disks.get(i));
}
}
public void moveDisks(int n, Tower destination, Tower buffer) {
if (n > 0) {
moveDisks(n - 1, buffer, destination);
moveTopTo(destination);
buffer.moveDisks(n - 1, destination, this);
}
}
}
publicstaticvoidmain(字符串[]args){
int n=5;
塔[]塔=新塔[n];
对于(int i=0;i<3;i++)塔[i]=新塔(i);
对于(int i=n-1;i>=0;i--)塔[0],添加(i);
塔[0]。移动磁盘(n,塔[2],塔[1]);
}
公务舱塔楼{
私有堆栈磁盘;
私有整数索引;
公共塔楼(内部一){
磁盘=新堆栈();
指数=i;
}
公共整数索引(){
收益指数;
}
公共无效添加(int d){
如果(!disks.isEmpty()&&disks.peek()=0;i--){
System.out.println(“+disks.get(i));
}
}
公共磁盘(int n、塔目标、塔缓冲区){
如果(n>0){
移动磁盘(n-1,缓冲区,目标);
移动到(目的地);
移动磁盘(n-1,目标,this);
}
}
}
提前感谢,,
Lin没有这样的规则,即磁盘从一根杆的顶部滑到下一根杆上。您可以将磁盘从一根杆移动到另一根杆上,只要
- 一次移动一个磁盘
- 较小磁盘的顶部不能放置任何磁盘
- 您可以将当前控制棒的上部磁盘移动到目标控制棒的顶部
- 一次移动一个磁盘
- 较小磁盘的顶部不能放置任何磁盘
- 您可以将当前控制棒的上部磁盘移动到目标控制棒的顶部
- 一次移动一个磁盘
- 较小磁盘的顶部不能放置任何磁盘
- 您可以将当前控制棒的上部磁盘移动到目标控制棒的顶部
- 一次移动一个磁盘
- 较小磁盘的顶部不能放置任何磁盘
- 您可以将当前控制棒的上部磁盘移动到目标控制棒的顶部
因此,它不要求塔在物理上彼此相邻。河内塔问题是一个使用递归的简单三步问题。有三根杆:
start , buffer and the destination
步骤1:将n-1个磁盘从起点移动到缓冲棒
步骤2:将第n个磁盘从起点移动到目标
步骤3:将n-1个磁盘从缓冲区移动到目标
也就是说,这可以很容易地用java编写
class Toh
{
public static void main(String []args)
{
int n=5;
toh(n,1,2,3);//move n disks from rod 1 to 3 using 2
}
public static void toh(int n,int start,int buffer,int destination)
{
if(n<1)
return;
toh(n-1,start,destination,buffer);//step 1
System.out.println("disk moved from rod "+start+" to "+destination);//step 2
toh(n-1,buffer,start,destination);//step 3
}
}
class-Toh
{
公共静态void main(字符串[]args)
{
int n=5;
toh(n,1,2,3);//使用2将n个磁盘从杆1移动到杆3
}
公共静态void toh(int n、int start、int buffer、int destination)
{
如果(n河内塔问题是一个使用递归的简单三步问题。有三个杆:
start , buffer and the destination
步骤1:将n-1个磁盘从起点移动到缓冲棒
步骤2:将第n个磁盘从起点移动到目标
步骤3:将n-1个磁盘从缓冲区移动到目标
也就是说,这可以很容易地用java编写
class Toh
{
public static void main(String []args)
{
int n=5;
toh(n,1,2,3);//move n disks from rod 1 to 3 using 2
}
public static void toh(int n,int start,int buffer,int destination)
{
if(n<1)
return;
toh(n-1,start,destination,buffer);//step 1
System.out.println("disk moved from rod "+start+" to "+destination);//step 2
toh(n-1,buffer,start,destination);//step 3
}
}
class-Toh
{
公共静态void main(字符串[]args)
{
int n=5;
toh(n,1,2,3);//使用2将n个磁盘从杆1移动到杆3
}
公共静态void toh(int n、int start、int buffer、int destination)
{
如果(n河内塔问题是一个使用递归的简单三步问题。有三个杆:
start , buffer and the destination
步骤1:将n-1个磁盘从起点移动到缓冲棒
步骤2:将第n个磁盘从起点移动到目标
步骤3:将n-1个磁盘从缓冲区移动到目标
也就是说,这可以很容易地用java编写
class Toh
{
public static void main(String []args)
{
int n=5;
toh(n,1,2,3);//move n disks from rod 1 to 3 using 2
}
public static void toh(int n,int start,int buffer,int destination)
{
if(n<1)
return;
toh(n-1,start,destination,buffer);//step 1
System.out.println("disk moved from rod "+start+" to "+destination);//step 2
toh(n-1,buffer,start,destination);//step 3
}
}
class-Toh
{
公共静态void main(字符串[]args)
{
int n=5;
toh(n,1,2,3);//使用2将n个磁盘从杆1移动到杆3
}
公共静态void toh(int n、int start、int buffer、int destination)
{
如果(n河内塔问题是一个使用递归的简单三步问题。有三个杆:
start , buffer and the destination
步骤1:将n-1个磁盘从起点移动到缓冲棒
步骤2:将第n个磁盘从起点移动到目标
步骤3:将n-1个磁盘从缓冲区移动到目标
也就是说,这可以很容易地用java编写
class Toh
{
public static void main(String []args)
{
int n=5;
toh(n,1,2,3);//move n disks from rod 1 to 3 using 2
}
public static void toh(int n,int start,int buffer,int destination)
{
if(n<1)
return;
toh(n-1,start,destination,buffer);//step 1
System.out.println("disk moved from rod "+start+" to "+destination);//step 2
toh(n-1,buffer,start,destination);//step 3
}
}
class-Toh
{
公共静态void main(字符串[]args)
{
int n=5;
toh(n,1,2,3);//使用2将n个磁盘从杆1移动到杆3
}
公共静态void toh(int n、int start、int buffer、int destination)
{
如果(nFeel free for any querys.@sumeethingh,querys for?@Jayesh,nice catch!:)可以自由查询。@sumeethingh,querys for?@Jayesh,nice catch!:)可以自由查询。@sumeethingh,querye