2014-03-18 05:28

题目:你肯定听过汉诺威塔的故事:三个柱子和N个从小到大的盘子。既然每次你只能移动放在顶上的盘子,这不就是栈操作吗?所以,请用三个栈来模拟N级汉诺威塔的玩法。放心,N不会很大的。

解法:递归着玩儿吧,还挺容易写的。要是迭代,我估计够呛。

代码:

 // 3.4 Implement Hanoi Tower with three stacks.
#include <cstdio>
#include <stack>
using namespace std; class Solution {
public:
void initHanoiTower(int n) {
int i; clearHanoiTower(); for (i = n; i >= ; --i) {
s[].push(i);
}
} void moveHanoiTower(int n, int from, int to) {
if (from == to) {
return;
} if ((int)s[from].size() < n) {
return;
} if (n == ) {
s[to].push(s[from].top());
s[from].pop();
return;
} int i;
for (i = ; i < ; ++i) {
b[i] = false;
}
b[from] = true;
b[to] = true;
int other;
for (i = ; i < ; ++i) {
if (!b[i]) {
other = i;
break;
}
} moveHanoiTower(n - , from, other);
moveHanoiTower(, from, to);
moveHanoiTower(n - , other, to);
} void clearHanoiTower() {
int i; for (i = ; i < ; ++i) {
while (!s[i].empty()) {
s[i].pop();
}
}
} void printHanoiTower() {
stack<int> ss[];
int i; for (i = ; i < ; ++i) {
ss[i] = s[i];
printf("Tower %d:", i + );
while (!ss[i].empty()) {
printf(" %d", ss[i].top());
ss[i].pop();
}
printf("\n");
}
}
private:
stack<int> s[];
int b[];
}; int main()
{
int n;
Solution sol; while (scanf("%d", &n) == && n > ) {
sol.initHanoiTower(n); printf("At the beginning:\n");
sol.printHanoiTower();
printf("\n"); sol.moveHanoiTower(n, , ); printf("At the end:\n");
sol.printHanoiTower();
printf("\n");
} return ;
}

《Cracking the Coding Interview》——第3章:栈和队列——题目4的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  6. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  7. 数据结构(c语言版,严蔚敏)第3章栈和队列

    第3章栈和队列

  8. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  9. Cracking the Coding Interview 150题(二)

    3.栈与队列 3.1 描述如何只用一个数组来实现三个栈. 3.2 请设计一个栈,除pop与push方法,还支持min方法,可返回栈元素中的最小值.pop.push和min三个方法的时间复杂度必须为O( ...

  10. 数据结构(C语言版)-第3章 栈和队列

    3.1 栈和队列的定义和特点3.2 案例引入3.3 栈的表示和操作的实现3.4 栈与递归3.5 队列的的表示和操作的实现3.6 案例分析与实现 基本操作有入栈.出栈.读栈顶元素值.建栈.判断栈满.栈空 ...

随机推荐

  1. FTP无法连接可能是安全狗设置的原因

    防火墙已添加某端口,但仍无法连接.可能是安全狗导致的. 如果安装了安全狗,请按照以下步骤设置:

  2. framework7对日历的一些效果处理

    现在的要求是日历中要区分已打卡和未打卡的显示,并且当月只显示当月的日历状态,其他月份不显示状态,并且打卡的日期不能大于当日 实现代码(精确到天): HTML: <div class=" ...

  3. 【UOJ139】【UER #4】被删除的黑白树(贪心)

    点此看题面 大致题意: 请你给一棵树黑白染色,使每一个叶结点到根节点的路径上黑节点个数相同. 贪心 显然,按照贪心的思想,我们要让叶结点到根节点的路径上黑节点的个数尽量大. 我们可以用\(Min_i\ ...

  4. 【CCPC-Wannafly Winter Camp Day4 (Div1) F】小小马(分类讨论)

    点此看题面 大致题意: 给你一张\(n*m\)的棋盘,问你一匹马在两个点中是否存在一条经过黑白格子数目相等的路径. 简化题目 首先,我们来简化一下题目. 考虑到马每次走的时候,所经过的格子的颜色必然发 ...

  5. 【BZOJ5212】[ZJOI2018] 历史(LCT大黑题)

    点此看题面 大致题意: 给定一棵树每个节点\(Access\)的次数,求最大虚实链切换次数,带修改. 什么是\(Access\)? 推荐你先去学一学\(LCT\)吧. 初始化(不带修改的做法) 首先考 ...

  6. E. New Reform_贪心,深搜,广搜。

    E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. EF Database first 中,实现 多个表对应一个 实体的 查询

    1.首先 创建好 数据 库. hobby表 major 表 student 表 外键 关系如下 2. 实现将 数据库 映射到EDM中 视图如下 在VS中 生成了 3个实体类  ,对应的是 数据库中的3 ...

  8. 第一个AngularJS指令

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. C# for语句

    一.C# for语句 for语句是C#语言中使用频率最高的循环语句. 1. for语句 语法格式如下: for(initializer; condition; iterator){    embedd ...

  10. Linux基础知识随笔记

    linux文件属性 ls -h human-readable以人类可读的形式显示 -i 显示inode号码 [root@oldboyedu55-bjb ~]# ls -ihl total 8.0K 3 ...