poj1920 Towers of Hanoi

关于汉诺塔的递归,记住一个结论是,转移n个盘子至少需要2^n-1步

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std; int two[100005],pos[100005]; int main()
{
int n,nn[5],i,j,ans,now,end,mid,a;
two[0]=1;
for(i=1;i<=100000;i++)
two[i]=(two[i-1]*2)%1000000;
while(~scanf("%d",&n))
{
scanf("%d%d%d",&nn[1],&nn[2],&nn[3]);
for(i=1;i<=3;i++)
{
for(j=1;j<=nn[i];j++)
{
scanf("%d",&a);
pos[a]=i;
}
}
ans=0;
end=now=pos[n];
printf("%d\n",pos[n]);
while(n>0)
{
if(end!=now)
{
ans=(ans+two[n-1])%1000000;
end=mid;
}
n--;
now=pos[n];
mid=6-now-end;
}
printf("%d\n",ans);
}
return 0;
}
上一篇:Hadoop Linux安装


下一篇:POJ1442-查询第K大-Treap模板题