TopCoder problem "TheEasyChase" used in SRM 423 (Division I Level Two , Division II Level Three)



Problem Statement

    

Two players play a simple game on a n x n board. The first player has a single white checker which is initially located at (rowWhite, colWhite). The second player has a single black checker which is initially located at (rowBlack, colBlack). All coordinates are 1-based. The two players alternate turns, and the first player moves first.

When it is the first player's turn, he chooses one of four directions (up, down, left or right) and moves his checker one cell in the chosen direction. When it is the second player's turn, he also chooses one of those four directions and moves his checker one or two cells in the chosen direction. A player wins the game when his move puts his checker in the cell occupied by his opponent's checker.

Both players use an optimal game strategy. If the player can win, he will follow the strategy that minimizes the number of moves in the game. If the player cannot win, he will follow the strategy that maximizes the number of moves in the game.

If the first player will win, return "WHITE x", and if the second player will win, return "BLACK x", where x is the number of moves in the game (all quotes for clarity).

 

Definition

    
Class:TheEasyChase
Method:winner
Parameters:int, int, int, int, int
Returns:String
Method signature:String winner(int n, int rowWhite, int colWhite, int rowBlack, int colBlack)
(be sure your method is public)
    
 

Constraints

-n will be between 2 and 20, inclusive.
-rowWhite will be between 1 and n, inclusive.
-colWhite will be between 1 and n, inclusive.
-rowBlack will be between 1 and n, inclusive.
-colBlack will be between 1 and n, inclusive.
-(rowWhite, colWhite) and (rowBlack, colBlack) will represent different cells.
 

Examples

0)
    
2
1
1
2
2
Returns: "BLACK 2"
There are two possible moves for the first player. But he will lose the game anyway.
1)
    
2
2
2
1
2
Returns: "WHITE 1"
Just one move in this game.
2)
    
3
1
1
3
3
Returns: "BLACK 6"

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13514&pm=9977

Writer:

Vasyl[alphacom]

Testers:

PabloGilberto , bmerry , Olexiy , ivan_metelsky

Problem categories:

Dynamic Programming