我无法在C中打印河内塔。
我的猜测是造成这种情况的原因是算法正在以递归方式运行。并且变量pegSource,pegSpare和pegDest正在开启。导致打印不正确。
当输入为3时,我当前的输出是:
| | | | | | | | | | | | | | | +|+ | | ++|++ | | +++|+++ | | XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | ++|++ | | +++|+++ | +|+ XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | | | | +|+ ++|++ +++|+++ XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | | | +|+ +++|+++ | ++|++ XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | +|+ | | ++|++ | +++|+++ XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | | | | ++|++ +++|+++ +|+ XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | | ++|++ | +|+ +++|+++ | XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | +|+ | | ++|++ | | +++|+++ | XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX
所需输出的位置为:
| | | | | | | | | | | | | | | +|+ | | ++|++ | | +++|+++ | | XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | ++|++ | | +++|+++ +|+ | XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | | | | +++|+++ +|+ ++|++ XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | | | +|+ +++|+++ | ++|++ XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | | | +|+ | +++|+++ ++|++ XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | | | | +|+ +++|+++ ++|++ XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | | | | ++|++ | +|+ +++|+++ | XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX | | | | | | | | | | | | | | | | +|+ | | ++|++ | | +++|+++ | XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX
这是我的C代码:
我已经尝试将PrintPegs(pegSource, pegDest, pegSpare);
行放在TOH函数中的几个位置以及MoveDisk函数中。当然没有成功。
如果可能的话,我希望能够打印上面所需的输出而不是我当前的输出。
#include <stdio.h>
#include <stdlib.h>
void PrintPegLine(int layer, int peg[]){
switch (peg[layer]){
case 0:
printf(" | ");
break;
case 1:
printf(" +|+ ");
break;
case 2:
printf(" ++|++ ");
break;
case 3:
printf(" +++|+++ ");
break;
case 4:
printf(" ++++|++++ ");
break;
case 5:
printf(" +++++|+++++ ");
break;
case 6:
printf(" ++++++|++++++ ");
break;
case 7:
printf(" +++++++|+++++++ ");
break;
case 8:
printf(" ++++++++|++++++++ ");
break;
default:
printf(" XXXXXXXXXXXXXXXXXXX ");
break;
}
}
void PrintPegs(int pegSource[], int pegDest[], int pegSpare[]) {
int layer;
for (layer=0; layer<9; layer++){
PrintPegLine(layer, pegSource);
PrintPegLine(layer, pegDest);
PrintPegLine(layer, pegSpare);
printf("\n");
}
}
void MoveDisk(int pegSource[], int pegDest[]){
int i,j;
int temp;
for (i=0; i<9; i++){
if (pegSource[i] != 0){
temp = pegSource[i];
pegSource[i]=0;
break;
}
}
for (j=0; j<9; j++){
if (pegDest[j] != 0){
pegDest[j-1]=temp;
break;
}
}
}
void TOH(int n, int pegSource[], int pegDest[], int pegSpare[]) {
if (n > 0) {
TOH(n - 1, pegSource, pegSpare, pegDest);
PrintPegs(pegSource, pegDest, pegSpare);
MoveDisk(pegSource, pegDest);
TOH(n - 1, pegSpare, pegDest, pegSource);
}
}
int main(int argc, char **argv)
{
int n = atoi(argv[1]);
if (n>8 || n<2){
printf("invalid input\n");
exit(0);
}
int pegSource[9] = {0,0,0,0,0,0,0,0,9};
int pegDest[9] = {0,0,0,0,0,0,0,0,9};
int pegSpare[9] = {0,0,0,0,0,0,0,0,9};
int i;
int j = 1;
for (i=8-n; i<8; i++){
pegSource[i]=j;
j++;
}
TOH(n, pegSource, pegDest, pegSpare);
PrintPegs(pegSource, pegDest, pegSpare);
printf("DONE\n");
return 0;
}
由于
答案 0 :(得分:1)
你解决河内塔的算法很好。问题在于您如何致电PrintPegs()
。当您递归时,您正确地置换了pegSource
,pegSpare
和pegDest
的顺序 - 但这也会排列它们打印出来的顺序,这是您不想要的
快速解决方法是将数组传递两次,一次进行置换,一次打印,如下所示:
void TOH(int n, int pegSource[], int pegDest[], int pegSpare[], int print1 [], int print2 [], int print3[]) {
if (n > 0) {
TOH(n - 1, pegSource, pegSpare, pegDest, print1, print2, print3);
PrintPegs(print1, print2, print3);
MoveDisk(pegSource, pegDest);
TOH(n - 1, pegSpare, pegDest, pegSource, print1, print2, print3);
}
}
int main(int argc, char **argv)
{
int n = atoi(argv[1]);
int pegSource[9] = {0,0,0,0,0,0,0,0,9};
int pegDest[9] = {0,0,0,0,0,0,0,0,9};
int pegSpare[9] = {0,0,0,0,0,0,0,0,9};
int i;
int j = 1;
if (n>8 || n<2){
printf("invalid input\n");
exit(0);
}
for (i=8-n; i<8; i++){
pegSource[i]=j;
j++;
}
TOH(n, pegSource, pegDest, pegSpare, pegSource, pegDest, pegSpare);
PrintPegs(pegSource, pegDest, pegSpare);
printf("DONE\n");
return 0;
}
这样做,我得到输出:
| | |
| | |
| | |
| | |
| | |
+|+ | |
++|++ | |
+++|+++ | |
XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX
| | |
| | |
| | |
| | |
| | |
| | |
++|++ | |
+++|+++ +|+ |
XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX
| | |
| | |
| | |
| | |
| | |
| | |
| | |
+++|+++ +|+ ++|++
XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX
| | |
| | |
| | |
| | |
| | |
| | |
| | +|+
+++|+++ | ++|++
XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX
| | |
| | |
| | |
| | |
| | |
| | |
| | +|+
| +++|+++ ++|++
XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX
| | |
| | |
| | |
| | |
| | |
| | |
| | |
+|+ +++|+++ ++|++
XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX
| | |
| | |
| | |
| | |
| | |
| | |
| ++|++ |
+|+ +++|+++ |
XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX
| | |
| | |
| | |
| | |
| | |
| +|+ |
| ++|++ |
| +++|+++ |
XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX
答案 1 :(得分:0)
我不确定是否可以按照您的意愿书写它。 因为每当你调用递归时,你实际上必须在挂钩之间切换,而不是永远不会将其切换回来。所以对于大多数递归调用,它实际上都是这样的
编辑:实际上,我开始认为如果你将使用另一个功能进行河内塔递归的最后一部分,你可以做到这一点