上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_20 Looking Ahead

◆交大密西根学院◆一 1817 UM-SITU Joint Institute University of Michigan Shanghai Jiao Tong University Vg101 Introduction to Computer and Programming Looking Forward in MATLAB
Vg101 Introduction to Computer and Introduction to Computer and Programming Programming Looking Forward in MATLAB

Tower of Hanoi Initial State A B Goal MoveTower A B Rules Can only move one disk at a time Not allowed to move a larger disk on top of a smaller one
Tower of Hanoi Tower of Hanoi A B C A B C Rules – Can only move one disk at a time – Not allowed to move a larger disk on top of a smaller one Initial State Goal MoveTower

Thinking Recursively MoveTower A B MoveTower MoveSingleDisk A B
Thinking Recursively Thinking Recursively A B C A B C MoveTower MoveSingleDisk MoveTower

◆交大密西根学院 1817 UM-SJTU Joint Institute University of Michigan Shanghai Jiao Tong University Thinking Recursively (cont.) A B This way,we have decomposed the problem of n disks into three simpler subproblems: 1. Move the top N-I disks (a sub-tower)from A to C 2.Move a single remaining disk from A to B 3.Move the N-I disks (a sub-tower)from C to B
Thinking Recursively (cont.) Thinking Recursively (cont.) • This way, we have decomposed the problem of n disks into three simpler subproblems: 1. Move the top N-1 disks (a sub-tower) from A to C 2. Move a single remaining disk from A to B 3. Move the N-1 disks (a sub-tower) from C to B A B C

◆交大密西根学院◆- 1817 UM-SJTU Joint Institute University of Michigan Shanghai Jiao Tong University Analyze the Recursion Condition Simple Cases: MoveSingleDisk(start spire,finish spire) Recursive Decomposition -Simpler subproblems:Tower with N-1 disks Same Form: MoveTower number of disks,start spire,finish spire,temporary spire)
Analyze the Recursion Condition Analyze the Recursion Condition • Simple Cases: – MoveSingleDisk(start spire, finish spire) • Recursive Decomposition – Simpler subproblems: Tower with N-1 disks – Same Form: • MoveTower ( number of disks, start spire, finish spire, temporary spire)

◆交大密西根学院一 宫8 1817 UM-SJTU Joint Institute University of Michigan Shanghai Jiao Tong University Write the Solution Void MoveTower (int n,char start,char finish,char tmp) { if (n ==1) { MoveSingleDisk(start,finish); else MoveTower(n-1,start,tmp,finish); MoveSingleDisk(start,finish); MoveTower(n-1,tmp,finish,start); 3 Done !!
Write the Solution Write the Solution Void MoveTower (int n, char start, char finish, char tmp) { if (n == 1) { MoveSingleDisk(start, finish); } else { MoveTower(n-1, start, tmp, finish); MoveSingleDisk(start, finish); MoveTower(n-1, tmp, finish, start); } } Done !!!

◆交大密西根学院◆ 宫8 1817 UM-SJTU Joint Institute University of Michigan Shanghai Jiao Tong University Seems Like Magic? Trust the recursive leap of faith: -All you need to do is: breaking a problem down into smaller subproblems of the same form and then providing solutions for the simple cases Trust the recursive process without going into the details how it works
Seems Like Magic ? Seems Like Magic ? • Trust the recursive leap of faith: – All you need to do is: • breaking a problem down into smaller subproblems of the same form and • then providing solutions for the simple cases – Trust the recursive process without going into the details how it works

Towers of Hanoi Case:n-4 (at the beginning)

Towers of Hanoi Case:n=4 (when finished)

Lets Us Trace the Solution Step 1 Move from Sre to Aux ■工>
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_19 Recursion 1.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_16 MATLAB environment short.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_15 Introduction to matlab.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_Chapter 1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第二章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_第三章习题与答案(第三版).doc
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter8 Views, Indexes.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter7 Constraints and Triggers.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter6 The database Language SQL –as a tutorial.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter5 Algebraic and Logic Query languages.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter4 High-level Database Models.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter3 Design Theory for Relational Databases.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)chapter11 The semi-structured data model Structured data.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》课程教学资源(课件讲稿)Chapter1 Introduction.pdf
- 上海交通大学:《数据库系统原理 The principle of Database System》教学资源_intro.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第二章 8086系统结构.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第一章 绪论(毛义梅).pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第四章 汇编语言程序设计_习题及解答.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第四章 汇编语言程序设计.pdf
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十章 串行通信和可编程接口芯片8251A_习题及解答.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Array and its Applications.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_examples on class design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Expressions and Statements.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_files_DataBase Design.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Function.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Computer and Programming.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Introduction to Vg101.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_objects and classes.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_programming style guide for C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Random Number_Graphics.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_Start with C plusplus.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Lecture Notes_vector_string.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation 1.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_recitation 13.pdf
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation IX.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation V.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation VIII.ppt
- 上海交通大学:《程序设计基础》课程教学讲义(密西根学院)Recitation Notes_Recitation X.ppt
- 上海交通大学:《微机原理与接口技术》课程教学资源(课件讲稿)第十四章 MCS-51单片机(1/2).pdf