Hanoi Tower求解

/*
    递归实现hanoi塔问题
 将盘子从x借助y移到z
*/
void CRecursion::hanoi(int count, string x, string y, string z)
{
 /*
 if(count == 1)
  cout<< "move " << count << " from " << x << " to " << z << endl;
 else
 {
  hanoi(--count, x, z, y);
  cout << "move " << count+1 << " from " << x << " to " << z << endl;
  hanoi(--count, y, z, x);
  cout << "move " << count+1 << " from " << y << " to " << z << endl;
 }
 */

 
 if(count == 1)
  cout<< "move " << count << " from " << x << " to " << z << endl;
 else
 {
  hanoi(count-1, x, z, y);
  cout << "move " << count << " from " << x << " to " << z << endl;
  hanoi(count-1, y, x, z);
 }
 
 
}

void main()

{

/////////////////////////////////////////////
 ////////     递归实现hanoi塔    ////////////
 int count = 3;
 string x = "one", y = "two", z = "three";
 CRecursion m_Recursion;
 m_Recursion.hanoi(count, x, y, z);

}

你可能感兴趣的:(Hanoi Tower求解)