Connection Status:
Competition Arena > LameKnight
SRM 380 · 2007-12-04 · by gevak · Greedy
Class Name: LameKnight
Return Type: int
Method Name: maxCells
Arg Types: (int, int)
Problem Statement

Problem Statement

A lame knight is located at the bottom-left corner of a height x width chessboard. Unlike a healthy knight, a lame knight can only make moves where he goes to the right. The only possible moves are:

  1. 2 cells up, 1 cell right;
  2. 1 cell up, 2 cells right;
  3. 1 cell down, 2 cells right;
  4. 2 cells down, 1 cell right.
The knight will make one trip, and he wants to maximize the number of visited cells. The knight's starting cell counts toward this number. There is also one restriction for the knight's trip: it must contain each kind of a move at least once, unless it is shorter than 4 moves. If the knight makes less than 4 moves (thus visiting less than 5 cells), his moves are not limited in any way. Return the maximal number of cells the knight can visit during one trip, including the initial cell.

Constraints

  • height will be between 1 and 2,000,000,000, inclusive.
  • width will be between 1 and 2,000,000,000, inclusive.
Examples
0)
100
50
Returns: 48

1 move of kind 2, 1 move of kind 3, 23 moves of kind 1 and 22 moves of kind 4.

1)
1
1
Returns: 1

There are no possible moves here, so the only visited cell is the starting cell.

2)
17
5
Returns: 4

It's possible to visit 5 cells (making 4 moves of kind 1 for example), but it's impossible to make it by 4 different moves. So, the best strategy here is to make 3 moves (thus visiting 4 cells).

3)
3
2000000000
Returns: 1999999998
4)
2000000000
2000000000
Returns: 1999999998
33)
20
4
Returns: 4

4 cells can be visited using 2 moves of kind 1 and 1 move of kind 4.

Submissions are judged against all 111 archived test cases, of which 6 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class LameKnight with a public method int maxCells(int height, int width) · 111 test cases · 2 s / 256 MB per case

Submitting as anonymous