资源介绍
// helloworld.cpp : Defines the entry point for the console application.
//
//by 陈墨仙 2019-07-18
//完全不用递归解汉诺塔
#include "stdafx.h"
#include
int h[34];//为了便于理解,0号元素不用,33个盘子
int a[4][34];
int b[4];
int jihao;
int dijici;
void printH()
{
//system("cls");
for(int i = 1;i<=34;i++)
{
printf("%d:%d\t",i,a[3][i]);
}
}
bool jiancha(int * h)//检测是否在下面的都是编号小的
{
int d[4];
d[1]=0;
d[2]=0;
d[3]=0;
for(int j = 1;j<4;j++)
{
for(int i = 1;i<34;i++)
{
if(a[j][i]==0 || a[j][i]>a[j][i-1])
{
}
else
{
printf("error a[%d][%d] = %d a[%d][%d]=%d",j,i-1,a[j][i-1],j,i,a[j][i]);
scanf("%d");
return false;
}
}
}
return true;
}
bool shunxu(int zhuzi)
{
for(int i = 1; i< b[zhuzi];i++)
{
if(a[zhuzi][i]==34-i)
{
}
else
{
return false;
}
}
return true;
}
bool chenggong(int n,int zhuzi)//n号盘是否都移到3
{
int d = 0;
int t = 34;
for(int i = 33;i>33-n;i--)
{
if(a[zhuzi][34-i]!=i-n+1)
{
return false;
}
}
return true;
}
int jc3()//检测3号柱的盘子有几个
{
int d = 0;
int t = 34;
for(int i = 33;i>0;i--)
{
if(h[i]==3)
{
d=d+1;
t = i;
}
}
return d;
}
int jc1()//检测1号柱的盘子有几个
{
int d = 0;
int t = 34;
for(int i = 33;i>0;i--)
{
if(h[i]==1)
{
d=d+1;
t = i;
}
}
return d;
}
void initH()
{
for(int i = 0;i<34;i++)
{
h[i]=1;
a[1][i]=i;
a[2][i]=0;
a[3][i]=0;
}
b[1]=34;
b[2]=1;
b[3]=1;
}
int jc2()//检测2号柱从33往下盘子有几个
{
int d = 0;
int t = 34;
for(int i = 33;i>0;i--)
{
if(h[i]==2)
{
d=d+1;
t = i;
}
}
return d;
}
int getTop(int zhuzi)
{
int d = 0;
int t = 0;
for(int i = 1;i<34;i++)
{
if(h[i]==zhuzi && i>t)
{
t = i;
}
}
return t;
}
bool jiou(int s)
{
if(s % 2 == 0)
{
return true;
}
else
{
return false;
}
}
void change(int i,int yuan,int mubiao)
{
if(h[i] == yuan)
{
h[i] = mubiao;
a[mubiao][b[mubiao]]=i;
b[mubiao]=b[mubiao]+1;
a[yuan][b[yuan]]=0;
b[yuan]=b[yuan]-1;
printf("h[%d]:%d->%d",i,yuan,mubiao);
}
else
{
printf("Error h[%d] = %d",i,h[i]);
scanf("%d");
}
jiancha(h);
}
int FastLog2(int x)
{
float fx;
unsigned long ix, exp;
fx = (float)x;
ix = *(unsigned long*)&fx;
exp = (ix >> 23) & 0xFF;
return exp - 127;
}
int chu2(int n ,int cishu)
{
for(int j = 1; j<= cishu;j++)
{
n=n/2;
}
return n;
}
void jihaopan(int n)
{
int i = 1;
int yuan = n;
while(1)
{
if(n%2==1)
{
jihao = i;
dijici = chu2(yuan,i)+1;
break;
}
n=n/2;
i=i+1;
}
//printf("几号盘%d,第几次%d",jihao,dijici);
}
int main(int argc, char* argv[])
{
printf("汉诺塔!\n");
int times = 0;
initH();
int ji = 1;
for(int i = 1;i<5559060534555523;i++)
{
jihaopan(i);
if(jiou(jihao)==false)
{
int tmp;
tmp = dijici%3;
if(tmp==1)
{
change(getTop(1),1,3);
}
else if(tmp==2)
{
change(getTop(3),3,2);
}
else
{
change(getTop(2),2,1);
}
}
else
{
int tmp;
tmp = dijici%3;
if(tmp==1)
{
change(getTop(1),1,2);
}
else if(tmp==2)
{
change(getTop(2),2,3);
}
else
{
change(getTop(3),3,1);
}
}
if(chenggong(33,3))
{
printf("sssss");
break;
}
}
return 0;
}
- 上一篇: 瓷器盘子效果
- 下一篇: 3D MAX室内装饰模型 盘子