0114. The King and the Rook

Input file name: kingrook.in
Output file name: kingrook.out
Time limit: 2 s
Memory limit: 64 megabytes

You are given a small chessboard 6*6. There are three pieces on it: a White King, a White Rook and a Black King. Your task is to calculate the minimal number of moves required for both sides to checkmate the Black King or determine that it is impossible or that the position is incorrect.

Input file

The only line of the input file contains three coordinates of fields – coordinates of White King, White Rook and Black King respectively, followed by an identifier of the side to move (W for white or B for black).

Output file

Write to the output file total number of moves (for both sides) required for white to win the game. If the black king is already checkmated, output 0. If the input position is incorrect (two kings are adjacent or it is white move and the check is declared for black), output -1. If the game will end in a draw (e.g. black are stalemated), output -2.

Examples:

kingrook.inkingrook.out
c6 f4 a5 B2
c6 f4 b5 W-1


Source: Petrozavodsk Summer 2003. Primary Contest, Sunday, August 24
Author: Andrew Lopatin, Nick Durov

Discuss       Submit a solution



Printable version