CS2040_Tutorial10_Qns
CS2040_Tutorial10_Qns
If U = 9999, L = 0000, R = 1 and the value of the button is 1000, then there is no way to
unlock the FeruLock, so output “Permanently locked”.
If U = 9999, L = 0000, R = 1 and the value of the button is 0001, then the minimum number
of button presses to unlock the FeruLock will be 9999.
If U = 1212, L = 5234, R = 3 and the values of the buttons are 1023, 0101 and 0001
respectively, then the minimum number of button presses will be 48.
You work in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the
maze neglected to create a fire escape plan. You need to escape QUICKLY. Given your location
in the maze and which squares (can be zero, one, or more than one) of the maze that are initially
on fire (when you first realized that there is a fire), you must determine whether you can exit the
maze before the fire reaches you; and if you can, how fast you can do it.
Both you and any of the fire each move one square per minute, vertically or horizontally (not
diagonally). The fire spreads in all four directions from each square that is on fire. You may exit
1
the maze from any square that borders the edge of the maze. Neither you nor the fire may enter a
square that is occupied by a wall.
You are given two integers R and C (1 ≤ R, C ≤ 1000) and a 2D character array M with R rows
and C columns that describes the starting condition of the maze. Each cell can contain either:
‘#’, a wall. Neither you nor the fire may enter this cell.
‘Y’, your initial position in the maze, which is a passable square. There will be exactly one
‘Y’ cell only in the test case.
‘F’, a square that is currently on fire at this point of time. There can be zero, one, or more
than one ‘F’ cell(s) in the test case.
Your task is to implement the following function EscapePlan that takes in R, C, M and simply
returns -1 if you cannot exit the maze before the fire reaches you, or a positive integer that gives
the earliest time that you can safely exit the maze, in minutes.
Example 1: R1 = 4, C1 = 4, maze M1 is as shown below
The function EscapePlan(R1, C1, M1) will return 3 because by running downwards three steps
(in three minutes), you can escape the maze while the fire cannot catch you.
Example 2: R2 = 3, C2 = 6, maze M2 is as shown below
The function EscapePlan(R2, C2, M2) will return -1 because after 1 minute, the nearest fire
already surrounds you. Note that if you and the fire reach the same cell at the same time, you die.
Example 3: R3 = 3, C3 = 3, maze M3 is as shown below
2
The function EscapePlan(R3, C3, M3) will return 2.
Example 4: R4 = 5, C4 = 5, maze M4 is as shown below
The function EscapePlan(R4, C4, M4) will return 3, a narrow escape as shown above.
Problem 2.a. What do the vertices and the edges of the graph represent?
Problem 2.c. Give the most appropriate graph algorithm (or modified version of it) to solve
this problem.
Given n currencies and m exchange rates, determine if we can start with a certain amount of money
in one currency, exchange this amount for other currencies, and end up at the same currency but
with more money than what we had at first. Two examples are as follows:
Suppose the money changer has n = 3 currencies and m = 3 exchange rates: 1 USD gives us
0.8 Euro; 1 Euro gives us 0.8 GBP (British pound sterling); and 1 GBP gives us 1.7 USD.
So if we start with 1 USD, we can exchange it for 0.8 Euro, which can then be exchanged for
0.64 GBP, and if converted back to USD we have 1.088 dollars. We have just make a profit
of 8.8 cents.
3
Example 2: If the money changer has the following n = 2 currencies and m = 2 exchange
rates: 1 USD gives us 0.8 Euro; and 1 Euro gives us 1.25 USD, then there is no way we can
make a profit.
Can you model the n currencies and their m exchange rates as a graph problem and give an
algorithm to report whether it is possible to start with any currency, exchange it with one (or
more) other currencies, end with the same starting currency, and make a profit? State the time
complexity of your algorithm.
Cats like to sit in high places. It is not uncommon to see cats climbing trees or furniture in order
to lie on the top-most area within their feline reach. Rar the Cat is no exception. However, he
does not know how high is one area relative to another.
Height can be measured in centimeters (cm) above sea level but Rar the Cat does not know the
absolute height of any place. However, he knows that area Bi will be higher than area Ai by
Hi centimetres because he needs to jump Hi to get from area Ai to Bi . There will be N areas in
total with N −1 such descriptions. Areas are labelled from 1 to N and 0 < A, B ≤ N where A 6= B.
Rar the Cat also has Q queries, each consisting 2 integers X and Y . He wants to know the height
of area Y with respect to area X. Do note that 0 < X, Y ≤ N but X can be equal to Y . In the
event that area Y is lower than area X, please output a negative number. Otherwise, output a
positive number.
It is guaranteed that the relative heights of all pairs of areas can be computed from the data provided
in the input. Design an algorithm that takes in the N areas and N − 1 descriptions, and
outputs the correct relative height for all Q queries efficiently. Your time complexity
should be dependent on both N and Q.