forked from keshavnandan/Topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneralChess.txt
48 lines (31 loc) · 2.47 KB
/
GeneralChess.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
PROBLEM STATEMENT
You have decided that too many people do not know how to play chess. So, in an effort to teach the rules you must write some software that helps to understand how chess-pieces affect one another. Your current project involves the knight and its ability to threaten one or more pieces at once. The knight has an unusual style of "jumping" around the board. One move consists of traveling two squares in one of the four cardinal directions, followed by one square perpendicular to the original direction. For example, if a knight is on (0,0), it may move to (2,1), (2,-1), (1,2), (1,-2), (-2, 1), (-2,-1), (-1,2), or (-1,-2). In addition, if a piece is on any of those locations, it is threatened by the knight on (0,0).
You will be given a vector <string> pieces, where each element is a comma delimited set of coordinates. Every element in pieces is formatted as "<x-coordinate>,<y-coordinate>" (quotes and angle brackets for clarity). Calculate and return a vector <string> where each element represents a position from which a knight threatens every piece in pieces. Your return vector <string> must be in the same format as pieces and sorted in increasing order by the x-coordinate. If two sets of coordinates have the same x-coordinate, the one with the smaller y-coordinate must come first.
DEFINITION
Class:GeneralChess
Method:attackPositions
Parameters:vector <string>
Returns:vector <string>
Method signature:vector <string> attackPositions(vector <string> pieces)
CONSTRAINTS
-pieces will contain between 1 and 8 elements, inclusive.
-Each element in pieces will be formatted as "<x-coordinate>,<y-coordinate>" (quotes and angle brackets for clarity).
-Each <x-coordinate> will be an integer between -10000 and 10000, inclusive and will not contain leading zeros.
-Each <y-coordinate> will be an integer between -10000 and 10000, inclusive and will not contain leading zeros.
-Each element in pieces will be unique.
EXAMPLES
0)
{"0,0"}
Returns: { "-2,-1", "-2,1", "-1,-2", "-1,2", "1,-2", "1,2", "2,-1", "2,1" }
This location is threatened from eight different places.
1)
{"2,1", "-1,-2"}
Returns: { "0,0", "1,-1" }
A knight may be in two places such that both pieces are threatened. In chess, placing your pieces in such positions is usually undesirable when your opponent has a knight.
2)
{"0,0", "2,1"}
Returns: { }
3)
{"-1000,1000", "-999,999", "-999,997"}
Returns: { "-1001,998" }
No three pieces can ever be threatened by a knight from more than one position.