Question 10
OCR Computer Science Specimen Paper 2A level26 marks
On the specification
- (a)(i)
- 1.4 Data types, data structures and algorithms
- (a)(ii)
- 2.2 Problem solving and programming1.4 Data types, data structures and algorithms
- (b)(i)
- 1.4 Data types, data structures and algorithms2.2 Problem solving and programming
- (b)(ii)
- 2.3 Algorithms
Question text
10 The Towers of Hanoi is a classic puzzle. Disks are placed in order on a pole, the biggest disc at the bottom of the pole, the smallest disk at the top of the pole, on the first of three poles. The challenge is to get them to the third pole in the same order. First step Final step The disks can only be moved under the following rules: only one disk can be moved at a time a disk can only ever be placed on an empty pole or on top of a larger disk a larger disk can never be placed on a smaller disk. This is a valid move. This would be a valid second move. This would not be a valid third move (you can’t put a bigger disk on a smaller one.) This would be a valid third move. (a) Each disk can be represented by an integer denoting its size. So Can be represented by (i) Explain why you would use a stack rather than a queue to store the configuration of disks at each pole. [2] (ii) The tower class has the method push. It takes in the value of the disk to be pushed. It adds it to the top of the stack if it is a valid move. If it is not a valid move, the value of the disc is not added and the message ‘Invalid move’ is printed to the screen. The stack is implemented using an array called pole and an integer called pointer. Pointer represents the index of the array position at the top of the stack. class Tower private array pole[10] private pointer public procedure new() pointer=0 endprocedure public procedure push(diskValue) //Code for push method endprocedure endclass Write the pseudocode to go in the push method. Annotate your pseudocode with comments to show how it solves the problem. You are not expected to test for overflow. (b) One way to try to find a solution would be to generate a tree of possible moves until a solution is found. (i) A tree has been started below. Complete Layer 3 to show 4 possible moves. (ii) The search space represented by the tree could be searched using a depth first or breadth first search. Describe one advantage and one disadvantage of depth-first search compared with breadth-first search. Advantage: Disadvantage: …[4]