Class name: Spiral
Method name: numberAt
Parameters: int, int, int, int
Returns: int
Implement a class Spiral, which contains a method numberAt. numberAt fills a
nRows by nCols two-dimensional array with numbers 1..N (where N = nRows *
nCols) in a clockwise spiral direction. The method then returns the value at a
specified position (row,col) of that array.
The method signature is:
int numberAt(int nRows, int nCols, int row, int col); (be sure the method is
declared public)
Notes:
The number of rows in the two-dimensional array is equal to nRows.
The number of columns in the two-dimensional array is equal to nCols.
The upper left corner is located at (row, col) = (0, 0)
The lower right corner is located at (row, col) = (nRows-1, nCols-1)
The rectangular array is to be filled in the following manner:
Start at row=0, col=0 which is filled with the number 1
Movement is clockwise and follows the pattern right-down-left-up until it fills
the entire rectangular array.
After each move, the current location is filled with the value of the previous
location plus one.
Here is an example of a spiral with nRows=5, nCols=6:
01 02 03 04 05 06
18 19 20 21 22 07
17 28 29 30 23 08
16 27 26 25 24 09
15 14 13 12 11 10
*TopCoder will verify that 0 < nRows <= 100, 0 < nCols <= 100, 0 <= row <
nRows, and 0 <= col < nCols.
-If the spiral is nRows=5 by nCols=6, row=4, and col=5, numberAt returns 10.
-If the spiral is nRows=5 by nCols=6, row=2, and col=3, numberAt returns 30.
-If the spiral is nRows=100 by nCols=100, row=50, and col=50, numberAt returns
9999.
-If the spiral is nRows=100 by nCols=100, row=3, and col=72, numberAt returns
1234.
-If the spiral is nRows=1 by nCols=1, row=0, and col=0, numberAt returns 1.
|