Вы находитесь на странице: 1из 8

Alexandria University Faculty of Engineering

Computer and Systems Engineering Dept. First Year Data Structures Programming Assignment 1 Problem 1: Desertification th Due Date: Monday, 4 March, 2013

Problem Statement Desertification (the process of good land turning into desert) is a severe problem on Bob's island. Bob's island is a rectangular grid of cells. You are given a char[][] island that shows the current state of Bob's island. The j-th character of the i-th element of island is 'D' if cell in row i, column j of the grid is desert and is 'F' if this cell is forest. The desert spreads each year as follows: - If a cell is desert, it remains desert forever. - If a cell is forest and it is adjacent to at least one desert cell (in one of the four orthogonal directions), it becomes desert after one year. - Otherwise the cell remains forest for another year. Return the number of desert cells after T years.
One possible simple algorithm to solve this problem could be:

For every year For every row For every column If the cell (row, column) is D Convert the four cells in the four orthogonal directions to desert

This simple algorithm is expensive in terms of the basic operation which is checking if a cell is D or F. You are required to develop two methods, desertArea_Simple and desertArea_Better. The first method is the simple implementation discussed before. In the second one, you should develop you own idea. It should be better in term of number of comparisons. Definition Methods: Parameters: Returns: Method signatures: desertArea_Simple, desertArea_Better char**, int, int, int int int desertArea_Simple(char** island, int rowSize, int columnSize, int T) int desertArea_Better(char** island, int rowSize, int columnSize, int T)

Constraints - rowSize will be between 1 and 10 elements, inclusive.


-

columnSize will be between 1 and 10 elements, inclusive. Each character in island will be 'D' or 'F'. Island is square so each row has the same number of columns. T will be between 1 and 1,000,000,000, inclusive.

Examples
0)

{"FFF", "FDF", "FFF"} 1 Returns: 5


After one year, the island will be:

FDF

DDD FDF
1)

{"FFF", "FDF", "FFF"} 2 Returns: 9


All cells will be desert after two years. 2)

{"FFFFF", "FFDFF", "FFFFD", "FFFFF", "FFFFF"} 2 Returns: 17


In this example, the picture on the left represents the initial state for Bob's island. After two years, the island state will become the picture on the right (dark green represents forest and pale yellow represents desert). Thus, the number of desert cells after 2 years is 17.

3)

{"FFFFFF", "FFFFFF", "FFFFFF", "FFFFFF"} 1000000000 Returns: 0

4)

{"FFFFFDFFFF", "FDFDFFFFFF", "FFFFFFFFFD", "FFFFFFFFFF", "DDFFFFFFFF", "FFFFFFFFFD", "FFFFFFFFFF", "FFFFFFFDFF", "FFFFFFFDFF", "FFFFDDFFFF"} 3 Returns: 90

5)

{"FFFFFDFFFF", "FDFDFFFFFF", "FFFFFFFFFD", "FFFFFFFFFF", "DDFFFFFFFF", "FFFFFFFFFD", "FFFFFFFFFF", "FFFFFFFDFF", "FFFFFFFDFF", "FFFFDDFFFF"} 98765432 Returns: 100

Problem 2: Rainy Road

Problem Statement
Fox Ciel is going to take a path to meet her friends. The path is tiled with 1x1 square tiles. It is N tiles long and 2 tiles wide. If we imagine that the path is going from the left to the right, we can view it as a rectangle with 2 rows and N columns of tiles. The rows of the path are numbered 0 to 1 from top to bottom, and the columns of the path are numbered 0 to N-1 from left to right. Ciel starts at the tile in row 0, column 0. She has to reach the tile in row 0, column N-1. In each step, Ciel can move to an adjacent tile. Two tiles are adjacent if they share at least one point (a side or a corner). Because it rained yesterday, some tiles are covered by puddles of water. Ciel will not step on these tiles. You are given a char [][] road. The j-th character of i-th element is 'W' if a tile at i-th row of j-th column is covered by water, and '.' otherwise. Return the char "Y" if she can move to her destination without entering a tile which is filled with water. Otherwise, return "N".

Definition Parameters: char**, int Returns: char Method signature: char isReachable(char** road, int columnSize) Notes -The constraints guarantee that the starting tile and the destination tile are never covered by water. Constraints -road will contain exactly 2 rows. -Each row of road will contain between 2 and 50 characters, inclusive. In other words, 2 <= columnSize <= 50. -All rows of road will contain the same number of characters. -Each character of road will be either '.' or 'W'. -The first character and the last character of 0-th element of road will be '.'. Examples

0) {".W.." ,"...."} Returns: "Y" One of the possible ways is as follows. Here, 'F' is the tile occupied by Fox Ciel. "FW.." "...." ".W.." "F..." ".W.." ".F.." ".W.." "..F." ".W.F" "...." 1) {".W.." ,"..W."} Returns: "Y" 2) {".W..W.." ,"...WWW."} Returns: "N" 3) {".." ,"WW"} Returns: "Y" 4) {".WWWW." ,"WWWWWW"} Returns: "N" 5) {".W.W.W." ,"W.W.W.W"} Returns: "Y" 6) {".............................................W." ,".............................................W."}

Returns: "N" Problem 3: MinCostPalindrome Problem Statement A palindrome is a string that reads the same from left to right as it does from right to left. You are given a String s. The length of s is even. Each character of s is either 'o', 'x', or '?' Your task in this problem is to replace each occurrence of '?' in s with either 'o' or 'x' so that s becomes a palindrome. You are also given ints oCost and xCost. Replacing '?' with 'o' costs oCost, and replacing '?' with 'x' costs xCost. Return the minimum cost of replacing '?'s by 'x's and 'o's that turns s into a palindrome. If it is impossible to obtain a palindrome, return -1 instead. Definition Parameters: char*, int, int, int Returns: int Method signature: int getMinimum(char* s, int length, int oCost, int xCost)

Notes -You are not allowed to change an 'x' into an 'o' or vice versa. Constraints -s will contain between 2 and 20 characters, inclusive. In other words, 2 <= length <= 20. -The length of s will be even. -Each character of s will be either 'o' or 'x' or '?'. -oCost will be between 1 and 50, inclusive. -xCost will be between 1 and 50, inclusive. Examples 0) "oxo?xox?" 3 5 Returns: 8 The only way to produce a palindrome is to replace s[3] with 'x' and s[7] with 'o'. The first replacement costs 5, the second costs 3, so the total cost is 3+5=8. 1) "x??x" 9 4 Returns: 8

There are two ways to produce a palindrome here. The cheaper one is to change both '?'s to 'x's. This costs 4+4=8. Note that you are required to replace all '?'s. 2) "ooooxxxx" 12 34 Returns: -1 There is no '?' character, and s is not a palindrome. We have no way to change it into a palindrome. 3) "oxoxooxxxxooxoxo" 7 4 Returns: 0 There is no '?' character, and s is already a palindrome. Making no replacements does not cost anything. 4) "?o" 6 2 Returns: 6 5) "????????????????????" 50 49 Returns: 980 6) "o??oxxo?xoox?ox??x??" 3 10 Returns: 38 Problem 4: Filtering Problem Statement You recently got a job at a company that designs various kinds of filters, and today, you've been given your first task. A client needs a filter that accepts some objects and rejects some other objects based on their size. The requirements are described in the int[] sizes and the String outcome. If character i in outcome is 'A', then all objects of size sizes[i] must be accepted, and if character i is 'R', then all objects of size sizes[i] must be rejected. If an object's size does not appear in sizes, then it doesn't matter if it is accepted or rejected.

Unfortunately, your knowledge of filters is very limited, and you can only design filters of one specific kind called (A, B)-filters. Each such filter is characterized by two integers A and B. It accepts an object if and only if its size is between A and B, inclusive. You have excellent (A, B)-filter construction skills, so you can construct any such filter where 1 <= A <= B. If it is possible to construct an (A, B)-filter that fulfills all the requirements described in sizes and outcome, return a int[] containing the filter's parameters, where element 0 is A and element 1 is B. If there are several appropriate filters, choose the one that minimizes B - A. If there are no suitable filters, return an empty int[]. Definition Parameters: int[], char*, int, int* Returns: int which is 0 for successfully return and 1 otherwise. Method signature: int designFilter(int* sizes, char* outcome, int length, int* result)

Constraints -sizes will contain between 1 and 50 elements, inclusive. In other words, 1 <= length <= 50. -Each element of sizes will be between 1 and 100, inclusive. -All elements of sizes will be distinct. -outcome will contain the same number of characters as the number of elements in sizes. -Each character in outcome will be 'A' or 'R'. -outcome will contain at least one 'A' character. -result will contain the output array of two elements. Examples 0) {3, 4, 5, 6, 7} "AAAAA" result: {3, 7 } and it returns 0 Any filter with A <= 3 and B >= 7 will work in this case. Among them, A = 3 and B = 7 gives the minimal difference of B - A. 1) {3, 4, 5, 6, 7} "AARAA" result: { } and it returns 1 This is similar to the previous example, but objects of size 5 need to be rejected. It's impossible to achieve this using a single (A, B)-filter. 2) {3, 4, 5, 6, 7} "RAAAA" result: {4, 7 } and it returns 0 However, it's possible to reject only objects of size 3.

3) {68,57,7,41,76,53,43,77,84,52,34,48,27,75,36} "RARRRARRRARARRR" result: {48, 57 } and it returns 0 4) {26,81,9,14,43,77,55,57,12,34,29,79,40,25,50} "ARAAARRARARARAA" result: { } and it returns 1
Notes
- Use only C language - You should follow the name of the methods signatures strictly like specified in the Definition section - Test your implementation via providing test cases. Your tests will be graded based on coverage for the different cases. - Write the pseudo code of your algorithm as a comment in the code. - Report is not required

Вам также может понравиться