TopCoder problem "RollingBlock" used in SRM 260 (Division II Level Three)



Problem Statement

    

You are given a rows x cols chessboard, and a cuboid with dimensions 1 x 1 x 3 standing on its small side (1 x 1) on one of the squares of the chessboard. You are trying to move the cuboid to another square, by tilting the cuboid over one of its base sides to some neighboring squares. The example figure below shows a 1 x 1 x 3 cuboid standing on the lower left square (first image). After the first move it lies on its 1 x 3 side in the middle of the lowest row (second image). After the second move it lies on its 1 x 3 side in the middle of the second lowest row. Finally, after the third move it is standing on its 1 x 1 side on the rightmost square of the second lowest row.

You are given rows and cols, specifying the number of rows and columns of the board. Further, you are given a int[] start and a int[] target, specifying the beginning and target positions of the cuboid, respectively. Both start and target will have exactly two elements. The first specifies the row and the second the column of the position (both 0-based). At the beginning, the cuboid is standing on its 1 x 1 side on the square specified by start. You are to return the minimum number of moves needed to move the cuboid so that it is standing on its 1 x 1 side on the square specified by target. During movement of the cuboid, no part of it may ever be outside the board limits specified by rows and cols. If the cuboid can not be moved to the target position, return -1. If the cuboid already is at the target square at the beginning, return 0.

 

Definition

    
Class:RollingBlock
Method:minMoves
Parameters:int, int, int[], int[]
Returns:int
Method signature:int minMoves(int rows, int cols, int[] start, int[] target)
(be sure your method is public)
    
 

Constraints

-rows and cols will be between 1 and 100, inclusive.
-start and target will each have exactly 2 elements.
-The first element of start and target will be between 0 and rows - 1, inclusive.
-The second element of start and target will be between 0 and cols - 1, inclusive.
 

Examples

0)
    
4
5
{3, 0}
{2, 4}
Returns: 3
The example from the problem statement (with rows numbered from back to front, columns numbered from left to right).
1)
    
5
5
{2, 2}
{4, 0}
Returns: -1
The cuboid is at the center of a 5 x 5 board, tilting it to any side would bring part of the cuboid outside the board, so no move is possible.
2)
    
4
4
{3, 0}
{3, 3}
Returns: 5
Tilt the cuboid up, then three times to the right, then down to arrive at the target position.
3)
    
4
4
{3, 0}
{0, 3}
Returns: 10
First move the cuboid to {3, 3} in 5 moves (as in the previous example). With 5 further similar moves we can then move it to the target position.
4)
    
100
100
{4, 5}
{5, 97}
Returns: 47
5)
    
20
20
{7, 9}
{7, 9}
Returns: 0
The cuboid is already on the target square.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=4749

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=7994&pm=4749

Writer:

gepa

Testers:

PabloGilberto , lbackstrom , brett1479 , Olexiy

Problem categories:

Dynamic Programming