시간 제한메모리 제한제출정답맞힌 사람정답 비율
4 초 128 MB303794322.396%

문제

꽃밭에 N*N개의 꽃이 N행 N열로 심어져 있다. 이 꽃밭에는 메뚜기가 한 마리 있는데, 메뚜기는 모든 꽃의 꽃잎의 개수를 알고 있다.

메뚜기는 맨 처음에 R행 C열에 있는 꽃 위에 있고, 다음과 같은 규칙을 지키면서 최대한 많은 꽃을 방문하려고 한다.

  1. 메뚜기는 인접한 행 또는 열에 있는 꽃으로 점프할 수 있다. 만약, 인접한 행에 있는 꽃으로 이동할 때는, 적어도 두 열 이상 점프를 해야 한다. 또, 인접한 열에 있는 꽃으로 이동할 때는, 적어도 두 행 이상 점프를 해야 한다. 즉, (r1, c1)에서 (r2, c2)로 점프를 할 수 있으려면 다음과 같은 조건을 만족해야 한다.
    • |r1-r2| = 1인 경우 |c1-c2| > 1 또는
    • |c1-c2| = 1인 경우 |r1-r2| > 1
  2. 점프하려고 하는 칸에 있는 꽃의 꽃잎의 개수는 현재 있는 칸의 꽃잎의 개수보다 많아야 한다.

메뚜기가 최대 몇 개의 꽃을 방문할 수 있는지 구하는 프로그램을 작성하시오.

입력

첫째 줄에 N이 주어진다. (1 ≤ N ≤ 1500)

둘째 줄에 메뚜기가 가장 처음에 있는 위치 R과 C가 주어진다. (1 ≤ R, C ≤ N)

다음 N개 줄에는 꽃잎의 수가 주어진다. 꽃잎의 수는 1,000,000보다 작거나 같다.

출력

첫째 줄에 최대 몇 개의 꽃을 방문할 수 있는지 출력한다.

예제 입력 1

4
1 1
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7

예제 출력 1

4

예제 입력 2

5
3 3
20 16 25 17 12
11 13 13 30 17
15 29 10 26 11
27 19 14 24 22
23 21 28 18 13

예제 출력 2

21
W3sicHJvYmxlbV9pZCI6IjI5NjkiLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWJhNTRcdWI2OWNcdWFlMzAiLCJkZXNjcmlwdGlvbiI6IjxwPlx1YWY0M1x1Y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bmJzcDs8XC9wPlxyXG5cclxuPHA+Jm5ic3A7PFwvcD5cclxuIiwiaGludCI6IiIsIm9yaWdpbmFsIjoiMSIsImh0bWxfdGl0bGUiOiIwIiwicHJvYmxlbV9sYW5nX3Rjb2RlIjoiRW5nbGlzaCJ9XQ==

출처

Contest > Croatian Open Competition in Informatics > COCI 2008/2009 > Contest #1 5번

  • 문제를 번역한 사람: baekjoon
  • 잘못된 조건을 찾은 사람: rhs0266