A Very Long Hike

시간 제한6초메모리 제한2048 MB

요약
n x n 고도 행렬이 평면을 주기적으로 채울 때, 한 걸음 비용이 1에 고도 차를 더한 값일 때 1e20초 안에 도달할 수 있는 서로 다른 격자점의 수를 센다.
난이도

어려움10점 중 10점

유형
그래프, 최단 경로, 기하, 수학
정답자
아직 제출이 없습니다

문제

You are planning a hike in the Peneda-Gerês National Park in the north of Portugal. The park takes its name from two of its highest peaks: Peneda (1340 m) and Gerês (1545 m).

For this problem, the park is modelled as an infinite plane, where each position (x,y)(x, y), with xx, yy being integers, has a specific altitude. The altitudes are defined by an n×nn \times n matrix hh, which repeats periodically across the plane. Specifically, for any integers aa, bb and 0≤x,y<n0 ≤ x, y < n, the altitude at (x+an,y+bn)(x + an, y + bn) is h\[x]\[y]h\[x]\[y].

When you are at position (x,y)(x, y), you can move to any of the four adjacent positions: (x,y+1)(x, y + 1), (x+1,y)(x + 1, y), (x,y−1)(x, y - 1), or (x−1,y)(x - 1, y). The time required to move between two adjacent positions is 1+∣alt_1−alt_2∣1 + |\text{alt}\_1 - \text{alt}\_2|, where alt_1\text{alt}\_1 and alt_2\text{alt}\_2 are the altitudes of the current and destination positions, respectively.

Initially, your position is (0,0)(0, 0). Compute the number of distinct positions you can reach within 102010^{20} seconds. Your answer will be considered correct if its relative error is less than 10−610^{-6}.

입력

The first line contains an integer nn (2≤n≤202 ≤ n ≤ 20)—the size of the matrix describing the altitudes.

The following nn lines contain nn integers each. The (j+1)(j + 1)-th number on the (i+1)(i + 1)-th of these lines is h\[i]\[j]h\[i]\[j] (0≤h\[i]\[j]≤15450 ≤ h\[i]\[j] ≤ 1545)—the altitude of the position (i,j)(i, j).

출력

Print the number of distinct positions you can reach within 102010^{20} seconds. Your answer will be considered correct if its relative error is less than 10−610^{-6}.

예제3

  1. 예제 1

    입력
    2
    3 3
    3 3
    
    예상 출력
    2e+40
    
  2. 예제 2

    입력
    3
    0 0 0
    0 1545 0
    0 0 0
    
    예상 출력
    2e+40
    
  3. 예제 3

    입력
    4
    0 1 2 3
    5 6 7 4
    10 11 8 9
    15 12 13 14
    
    예상 출력
    1.524886878e+39