您现在的位置是:首页 > 后台技术 > 数据结构与算法数据结构与算法
分治算法汉诺塔(图文)
第十三双眼睛2023-10-22【数据结构与算法】人已围观
简介分治算法汉诺塔
分治算法汉诺塔
package com.xingchen.day016; public class Hanota { public static void main(String[] args) { hanota(5,'a', 'b', 'c'); } public static void hanota(int n,char a,char b,char c) { if (n == 1) { System.out.println(a + " -> " + c); } else { //将上方的n-1个盘子借助c,从a 移动到b hanota(n - 1, a, c, b); //把最下面的盘子从a移动到c System.out.println(a + " -> " + c); //把b的盘子移动到c,借助a hanota(n - 1,b ,a, c); } } } |
Tags:
很赞哦! ()
上一篇:非递归二分查找算法(图文)
下一篇:动态规划算法解决背包问题(图文)