Problem Statement
A topographical map is one which shows the elevation of the land, as well as important features like roads. In this problem, you will be given a topographical map, represented as a String[], topo. Character i of element j of topo will represent the height of the land at position (i,j). Here, 'A' represents a height of 0, 'B' a height of 1, and so on up to 'Z'. You will be given two ints, x, and y, representing some location (x,y) on the topographical map. You will also be given a String, direction, which will be "North", "East", "South", or "West". You are to return the location of the farthest point on the map that can be seen from (x,y) in that direction. North is in the negative y direction, and east is in the positive x direction. You should assume that the slope of the topography between adjacent points is constant.
A point can be seen if and only if none of the points between it and (x,y) are high enough that they block the line of sight, looking from a point slightly (an infinitesimally small amount) above the ground at (x,y). Note that since you are slightly above the level of the ground at the point you are looking from, if the land were flat in the direction you were looking, (see example 4), you would see the point farthest from you. You should return a int[] with two elements, whose first element is the x coordinate of the point seen, and whose second element is the y coordinate of the point seen.
A point can be seen if and only if none of the points between it and (x,y) are high enough that they block the line of sight, looking from a point slightly (an infinitesimally small amount) above the ground at (x,y). Note that since you are slightly above the level of the ground at the point you are looking from, if the land were flat in the direction you were looking, (see example 4), you would see the point farthest from you. You should return a int[] with two elements, whose first element is the x coordinate of the point seen, and whose second element is the y coordinate of the point seen.
Definition
- Class:
- TopoView
- Method:
- getPeak
- Parameters:
- String[], int, int, String
- Returns:
- int[]
- Method signature:
- int[] getPeak(String[] topo, int x, int y, String direction)
- (be sure your method is public)
Notes
- When looking in a direction, you can only see points that are exactly in that direction.
Constraints
- topo will contain between 1 and 50 elements, inclusive.
- Each element of topo will contain between 1 and 50 uppercase letters ('A'-'Z'), inclusive.
- Each element of topo will contain the same number of characters.
- There will be at least one character in the direction specified (eg. if the location is (0,0), direction may not be "North").
- direction will be "North", "East", "South", or "West".
- x will be between 0 and the number of characters in elements of topo - 1, inclusive.
- y will be between 0 and the number of elements in topo - 1, inclusive.
Examples
{"ABCD", "CBDF", "ACGE", "BEDB"}0
1
"South"
Returns: { 0, 3 }Here, the farthest point we can see is a 'B' at (0,3).{"ABCD", "CBDF", "ACGE", "BEDB"}1
1
"South"
Returns: { 1, 3 }From (1,1), the farthest point we can see is an 'E' at (1,3){"ABCD", "CBDF", "ACGE", "BEDB"}1
1
"East"
Returns: { 3, 1 }Keep in mind that we are looking from a point slightly above the ground.{"ABDD", "CBGF", "ACDE", "BECB"}2
2
"North"
Returns: { 2, 1 }{"ABBBBB"}5
0
"West"
Returns: { 1, 0 }{"CBAA"}0
0
"East"
Returns: { 3, 0 }