Baltic OI '06 P6 - Jump

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 1.0s
Memory limit: 64M

Problem type
Allowed languages
Assembly, Brain****, C, C++, COBOL, Forth, Fortran, Lua, Text, Turing
Baltic Olympiad in Informatics: 2006 Day 2, Problem 3

An n \times n game board is populated with integers, one non-negative integer per square. The goal is to jump along any legitimate path from the upper left corner to the lower right corner of the board. The integer in any one square dictates how large a step away from that location must be. If the step size would advance travel off the game board, then a step in that particular direction is forbidden. All steps must be either to the right or toward the bottom. Note that a 0 is a dead end which prevents any further progress.

Consider the 4 \times 4 board shown in Figure 1, where the solid circle identifies the start position and the dashed circle identifies the target. Figure 2 shows the three legitimate paths from the start to the target, with the irrelevant numbers in each removed.

Figure 1
Figure 2

Your task is to write a program that determines the number of legitimate paths from the upper left corner to the lower right corner.

Constraints

4 \le n \le 100

In test cases worth 70\% of points, the answer will fit within a 64-bit integer.

It is guaranteed that all inputs will lead to an answer of no more than 100 digits.

Input Specification

The first line of input contains an integer n, which is the number of rows in this board. This is followed by n rows of data. Each row contains n integers, each one in the range 0 \dots 9.

Output Specification

Output a single integer: the number of legitimate paths from the upper left corner to the lower right corner.

Sample Input

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

Sample Output

3

Comments

There are no comments at the moment.