assignment 1

Upload: mohamed-hegazy

Post on 17-Oct-2015

9 views

Category:

Documents


0 download

DESCRIPTION

Very good

TRANSCRIPT

  • 5/27/2018 Assignment 1

    1/8

    Programming Assignment 1Problem 1: Desertification

    Due Date: Monday, 4thMarch, 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 islandis '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 Tyears.

    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: desertArea_Simple, desertArea_Better

    Parameters: char**, int, int, int

    Returns: int

    Method signatures: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 islandwill be 'D' or 'F'. Island is square so each row has the same number of

    columns.

    - Twill 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

    Alexandria UniversityFaculty of Engineering

    Computer and Systems Engineering Dept.First Year

    Data Structures

  • 5/27/2018 Assignment 1

    2/8

    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 willbecome the picture on the right (dark green represents forest and pale yellow represents desert). Thus, the number ofdesert 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)

  • 5/27/2018 Assignment 1

    3/8

    {"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 inrow 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

    -roadwill contain exactly 2 rows.

    -Each row of roadwill contain between 2 and 50 characters, inclusive. In other words, 2

  • 5/27/2018 Assignment 1

    4/8

    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."}

  • 5/27/2018 Assignment 1

    5/8

    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

    -swill contain between 2 and 20 characters, inclusive. In other words, 2

  • 5/27/2018 Assignment 1

    6/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 sis not a palindrome. We have no way to change it into a

    palindrome.

    3)

    "oxoxooxxxxooxoxo"

    7

    4

    Returns: 0

    There is no '?' character, and sis 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.

  • 5/27/2018 Assignment 1

    7/8

    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

  • 5/27/2018 Assignment 1

    8/8

    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