2
昨晚我一直在實施河內塔而不使用遞歸。我在維基頁面wiki上找到了關於同一主題的維基百科上的算法。 我已經實現了它,它工作正常(對於奇數:現在)。但是我對代碼不滿意,它的體積很大,因此我想以某種方式對代碼進行修改,以便實際的代碼行可以通過相同的功能和算法減少。河內塔的實施 - 迭代程序
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
display(int *A, int *B, int *C, int atop , int btop ,int ctop)
{
int i;
if(A[0]==0)
printf("\nEmpty A");
else
{
printf("\nA Stack :\n");
for (i=atop; i>=1; i--)
{
printf(" %d\n",A[i]);
}
}
if(B[0]==0)
printf("\nEmpty B\n");
else
{
printf("\nB Stack: \n");
for (i=btop; i>=1; i--)
{
printf(" %d\n",B[i]);
}
}
if(C[0]==0)
printf("\nEmpty C\n");
else
{
printf("\nC Stack: \n");
for (i=ctop; i>=1; i--)
{
printf(" %d\n",C[i]);
}
}
}
int main()
{
int step=0;
int n,i,*A,*B,*C,atop,btop,ctop,count=0;
int max=1;
printf("\nInput # disks");
scanf("%d",&n);
for(i=0; i<n; i++)
{
max*=2;
}
max=max-1;
printf("\n Max is :%d",max);
A= (int *)malloc(sizeof(int)*(n+1));
B= (int *)malloc(sizeof(int)*(n+1));
C= (int *)malloc(sizeof(int)*(n+1));
A[0]=B[0]=C[0]=0;
atop=btop=ctop=0;
for(i=n; i>0; i--)
{
atop++;
A[0]++;
A[atop]=i;
B[atop]=0;
C[atop]=0;
}
display(A,B,C,atop,btop,ctop);
do
{
count+=1;
step=(step%3)+1;
switch(step)
{
case 1://move between pegs A and C
printf("\n%d count:",count);
if(count%2 ==1)//move of disk 1
{
if(A[atop]==1)
{
ctop++;
C[ctop]=A[atop];
A[atop]=0;
atop--;
C[0]++;
A[0]--;
printf("\nDisk %d: A->C",C[ctop]);
} //i is on A
else if(C[ctop]==1)//1 is on b
{
atop++;
A[atop]=C[ctop];
C[ctop]=0;
ctop--;
A[0]++;
C[0]--;
printf("\nDisk %d: C->A",A[atop]);
}
}//Movement of disk #1
else if(count %2 ==0)
{
if(ctop !=0 && A[atop]>C[ctop])
{
//C[0]--;
atop++;
A[atop]=C[ctop];
C[ctop]=0;
ctop--;
C[0]--;
A[0]++;
printf("\nDisk %d: C->A",A[atop]);
}
else if (ctop ==0)
{
ctop++;
C[ctop]=A[atop];
A[atop]=0;
atop--;
C[0]++;
A[0]--;
printf("\n disk %d:A->C",C[ctop]);
}
else if(atop !=0 && C[ctop]>A[atop])
{
ctop++;
C[ctop]=A[atop];
A[atop]=0;
atop--;
C[0]++;
A[0]--;
printf("\nDisk %d: A->C",C[ctop]);
}
else if(atop==0)
{
atop++;
A[atop]=C[ctop];
C[ctop]=0;
ctop--;
C[0]--;
A[0]++;
printf("\nDisk %d: C->A",A[atop]);
}
}//Movement of Disk non1
break;
case 2://move between pegs A and B
printf("\n%d count:",count);
if(count%2 ==1)//move of disk 1
{
if(A[atop]==1)
{
btop++;
B[btop]=A[atop];
A[atop]=0;
atop--;
B[0]++;
A[0]--;
printf("\nDisk %d: A->B",B[btop]);
} //i is on A
else if(B[btop]==1)//1 is on b
{
atop++;
A[atop]=B[btop];
B[btop]=0;
btop--;
A[0]++;
B[0]--;
printf("\nDisk %d: B->A",A[atop]);
}
}//Movement of disk #1
else if(count %2 ==0)
{
if(btop !=0 && A[atop]>B[btop])
{
//B[0]--;
atop++;
A[atop]=B[btop];
B[btop]=0;
btop--;
B[0]--;
A[0]++;
printf("\nDisk %d: B->A",A[atop]);
}
else if (btop ==0)
{
btop++;
B[btop]=A[atop];
A[atop]=0;
atop--;
B[0]++;
A[0]--;
printf("\n disk %d:A->B",B[btop]);
}
else if(atop !=0 && B[btop]>A[atop])
{
btop++;
B[btop]=A[atop];
A[atop]=0;
atop--;
B[0]++;
A[0]--;
printf("\nDisk %d: A->B",B[btop]);
}
else if(atop==0)
{
atop++;
A[atop]=B[btop];
B[btop]=0;
btop--;
B[0]--;
A[0]++;
printf("\nDisk %d: B->A",A[atop]);
}
}//Movement of Disk non1
break;
case 3://move between pegs C and B
printf("\n%d count:",count);
if(count%2 ==1)//move of disk 1
{
if(C[ctop]==1)
{
btop++;
B[btop]=C[ctop];
C[ctop]=0;
ctop--;
B[0]++;
C[0]--;
printf("\nDisk %d: C->B",B[btop]);
} //i is on C
else if(B[btop]==1)//1 is on b
{
ctop++;
C[ctop]=B[btop];
B[btop]=0;
btop--;
C[0]++;
B[0]--;
printf("\nDisk %d: B->C",C[ctop]);
}
}//Movement of disk #1
else if(count %2 ==0)
{
if(btop !=0 && C[ctop]>B[btop])
{
//B[0]--;
ctop++;
C[ctop]=B[btop];
B[btop]=0;
btop--;
B[0]--;
C[0]++;
printf("\nDisk %d: B->C",C[ctop]);
}
else if (btop ==0)
{
btop++;
B[btop]=C[ctop];
C[ctop]=0;
ctop--;
B[0]++;
C[0]--;
printf("\n disk %d:C->B",B[btop]);
}
else if(ctop !=0 && B[btop]>C[ctop])
{
btop++;
B[btop]=C[ctop];
C[ctop]=0;
ctop--;
B[0]++;
C[0]--;
printf("\nDisk %d: C->B",B[btop]);
}
else if(ctop==0)
{
ctop++;
C[ctop]=B[btop];
B[btop]=0;
btop--;
B[0]--;
C[0]++;
printf("\nDisk %d: B->C",C[ctop]);
}
}//Movement of Disk non1
break;
default:
printf("Some Error!");
}//switch end
// display(A,B,C,atop,btop,ctop);
}
while((count <=max) && (C[0]!=n));
printf("\n");
//display(A,B,C,atop,btop,ctop);
display(A,B,C,atop,btop,ctop);
return 0;
}
請幫助我。
應該去http://codereview.stackexchange.com/? – fritzone 2014-08-28 12:32:03
感謝來源,在那邊問它。如果可能,請回復其他方法。謝謝。 – cracknut 2014-08-30 04:33:36