CS331 Advanced Data Structures – Spring 2016 Due: Feb. 1

advertisement
CS331 Advanced Data Structures – Spring 2016
Homework 1 – 50 points
Due: Feb. 1
1. (10 points)
(a) Determine the number of inversions of the sequence 5,3,9,2,1,0,8,4,6,7 with respect to
the sequence 0, 1, 2, . . . 9
(b) What would be the last number of inversions returned from our modified merge function
discussed in class?
(c) Assume we have a list of n items. Determine a relationship between the number of
inversions with respect to the ascending sorted list and the number of inversions with
respect to the descending sorted list.
2. (20 points) You are a real estate broker who has a square section of land you wish to sell.
The land is divided up into square sections, and you are selling them in groups of three, in
the following shape:
There is only one problem: one of the squares of land has been designated as wetlands, and
it is illegal for you or anyone else to build on it. Thus this square cannot be a part of any
of the 3-square parcels you are selling. Given the location of this wetland square, you would
like to find a way to divide the land into the 3-square parcels. For example, if the wetland
square is the x-ed out square in the first figure below, then one possible division of the land
is shown on the right:
@
@
@
@
Write a divide-and-conquer algorithm to solve this following problem. The input will consist
of three numbers: n (the side length of the square land to divide, always a power of 2), r and
c (the coordinates of wetlands square, each in the range 0 . . . n − 1). Given this, you should
output the grid with numbers indicating which squares are included in each of the 3-parcel
tracts of land. One possible output for the above example might be:
2 -1
2 2
3 1
3 3
4
1
1
5
4
4
5
5
PROGRAM HINT: Divide the parcel into 4 even size sections and determine which section
the wetlands square is, i.e. a square that should not be used. Then place a 3-parcel piece on
the grid in such a way that EACH of the 4 smaller sections now has a square that should not
be used.
(over)
3. (20 points) Write a backtracking program to read in and solve a crypto-arithmetic problem.
Input will be an integer n indicating the number of addends, followed by the n addends and
then the crypto-arithmetic problem, or indicate that there are no solutions. For example, the
input
2
SEND
MORE
MONEY
corresponds to the crypto-arithmetic problem
SEND
+ MORE
MONEY
Your solutions should look something like
Solution 1:
9567
+1085
----10652
On input
3
A
A
A
B
your solutions should look something like
Solution 1:
1
+1
+1
+1
-4
Solution 2:
2
+2
+2
+2
-8
The maximum value for n will be 5, and each string will be no more than 10 letters long. More
points will be given for a branch-and-bound solution as opposed to a straight backtracking
solution.
Download