回朔 汉诺塔hanoi('a','b','c') protected void hanoi(int n, char a, char b, char c) { if (n == 1)//把最后一个搬到C { Response.Write(string.Format("Move disk {0} from {1} to {2}<br/>", n, a, c)); } else { hanoi(n - 1, a, c, b);//将A往B搬运 Response.Write(string.Format("Move disk {0} from {1} to {2}<br/>", n, a, c)); hanoi(n - 1, b, a, c);//将B往C搬运 } }
汉诺塔hanoi('a','b','c') protected void hanoi(int n, char a, char b, char c)
{
if (n == 1)//把最后一个搬到C
{
Response.Write(string.Format("Move disk {0} from {1} to {2}<br/>", n, a, c));
}
else
{
hanoi(n - 1, a, c, b);//将A往B搬运
Response.Write(string.Format("Move disk {0} from {1} to {2}<br/>", n, a, c));
hanoi(n - 1, b, a, c);//将B往C搬运
}
}