/* -*- coding: utf-8 -*- * * 2855.cc: No.2855 Move on Grid - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_N = 300; const int MAX_M = 300; const int MAX_NM = MAX_N * MAX_M; const int MAX_A = 1000000000; const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1}; /* typedef */ using qi = queue; /* global variables */ int as[MAX_NM], ds[MAX_NM]; /* subroutines */ int calc(int n, int m, int x) { int nm = n * m; fill(ds, ds + nm, -1); ds[0] = (as[0] < x) ? 1 : 0; qi q0, q1; q0.push(0); while (! q0.empty()) { int u = q0.front(); q0.pop(); int uy = u / m, ux = u % m; for (int di = 0; di < 4; di++) { int vy = uy + dys[di], vx = ux + dxs[di], v = vy * m + vx; if (vy >= 0 && vy < n && vx >= 0 && vx < m && ds[v] < 0) { if (as[v] < x) { ds[v] = ds[u] + 1; q1.push(v); } else { ds[v] = ds[u]; q0.push(v); } } } if (q0.empty()) swap(q0, q1); } return ds[nm - 1]; } /* main */ int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); int nm = n * m; for (int i = 0; i < nm; i++) scanf("%d", as + i); int x0 = 1, x1 = MAX_A + 1; while (x0 + 1 < x1) { int x = (x0 + x1) / 2; if (calc(n, m, x) <= k) x0 = x; else x1 = x; } printf("%d\n", x0); return 0; }