MKJUMPS - Making Jumps

A knight is a piece used in the game of chess. The chessboard itself is square array of cells. Each time a knight moves, its resulting position is two rows and one column, or two columns and one row away from its starting position. Thus a knight starting on row r, column c – which we’ll denote as (r,c) – can move to any of the squares (r-2,c-1), (r-2,c+1), (r-1,c-2), (r-1,c+2), (r+1,c-2), (r+1,c+2), (r+2,c-1), or (r+2,c+1). Of course, the knight may not move to any square that is not on the board.

Suppose the chessboard is not square, but instead has rows with variable numbers of columns, and with each row offset zero or more columns to the right of the row above it. The figure to the left illustrates one possible configuration. How many of the squares in such a modified chessboard can a knight, starting in the upper left square (marked with an asterisk), not reach in any number of moves without resting in any square more than once?

subir imagenes

If necessary, the knight is permitted to pass over regions that are outside the borders of the modified chessboard, but as usual, it can only move to squares that are within the borders of the board.


There will be multiple cases to consider. The input for each case begins with an integer n, between 1 and 10, that specifies the number of rows in the modified chessboard. Following n there will be n pairs of integers, with the ith pair corresponding to the ith row of the chessboard. The first integer of each pair indicates the number of squares skipped at the beginning of the row. The second integer indicates the number of squares in the row (which will always be at least 1).The last case will be followed by the integer 0.

For example, input for the case illustrated by the chessboard shown above would be:

7 0 3 0 3 0 4 0 4 1 3 1 7 4 4

The maximum dimensions of the board will be 10 rows and 10 columns. That is, any modified chessboard specified by the input will fit completely on a 10 row, 10 column board.


For each input case, display the case number (1, 2, …), and the number of squares that the knight can not reach. Display the results in the format shown in the examples below.


7 0 3 0 3 0 4 0 4 1 3 1 7 4 4
3 0 3 0 3 0 3
2 0 1 2 1

Case 1, 4 squares can not be reached.
Case 2, 1 square can not be reached.
Case 3, 0 squares can not be reached.

hide comments
M Sudeep Kumar: 2014-09-25 15:44:24

what is the complexity?

Sudharsansai: 2014-08-15 10:05:02

Got in First Go:)
But trying to optimise it hardly..:)

AlcatraZ: 2014-04-10 16:45:06

@amaroq Thx for the description..
"You can only use one single route and visit as many squares as possible."

Last edit: 2014-04-10 16:47:19
Hector Navarro: 2013-12-06 22:19:17

I got AC, but I think I shouldn't, because I should get a TLE with the case:

10 0 10 0 10 0 10 0 10 0 10 0 10 0 10 0 10 0 10 0 10

My conclusion is that there are no big cases on the test cases.

Ankur Ahuja: 2013-09-08 21:18:39

Last edit: 2013-09-08 21:29:24
Kre¹imir Kralj: 2013-08-14 20:55:11

[10][10] -> WA,[11][11] AC..

Last edit: 2013-08-14 20:59:18
sagar goyal: 2013-06-17 20:21:17

nyc one...not too hard..
beware of words "square" & "squares"..:)

Last edit: 2013-06-17 20:22:38
shubham: 2013-05-26 06:51:17

@amaroq , hey i'm not getting how to solve this problem, if we start at any square on the board we will end up with all the squares occupied by knight then
how we will get any unoccupied spaces. Help!

Mohannad Abdullah Hassan: 2013-03-23 20:13:12

I could not solve the problem without your explanation.

I'd like to clarify that one shouldn't try to solve this problem by exploring all possible moves. Instead, try to solve the problem by applying a series of consecutive valid moves.

NSK SSY RYZ GY LC XC QDC ZH ZW ZWT ZSR MCX ST : 2011-09-13 01:28:08

Thanks a lot for your explanation

Added by:Camilo Andrés Varela León
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:North Central North America Regional Programming Contest - 2003