#include #include #include #include using namespace std; #define MAXH 1000000009 #define MAXR 1000 #define MAXC 1000 #define MAXK 1337 int r, c, k; int h[MAXR][MAXC]; int d[MAXR][MAXC]; struct qdata { int b, y, x; bool operator<( qdata o ) const { if( b == o.b ) if( y == o.y ) return x < o.x; else return y < o.y; else return b > o.b; } }; bool test( int t ) { // prepare datastructures priority_queue q; for( int x = 0; x < c; ++x ) { q.push(qdata{ 0, -1, x }); for( int y = 0; y < r; ++y ) d[y][x] = MAXK; } // bfs const int b[4][2] = { {1,0}, {-1,0}, {0,1}, {0,-1} }; while( not q.empty() ) { qdata a = q.top(); q.pop(); for( int i = 0; i < 4; ++i ) { int y = a.y+b[i][0], x = a.x+b[i][1]; if( x >= 0 and x < c and y >= 0 and y < r and d[y][x] == MAXK ) { d[y][x] = a.b + ( h[y][x] < t ); if( d[y][x] <= k ) { if( y == r-1 ) return true; q.push(qdata{ d[y][x], y, x }); } } } } return false; } int main() { // read canyon data cin >> r >> c >> k; for( int y = 0; y < r; ++y ) for( int x = 0; x < c; ++x ) cin >> h[y][x]; // binary search int hi = MAXH, lo = 0; while( hi-lo > 1 ) { int m = (lo + hi) / 2; ( test(m) ? lo : hi ) = m; } cout << lo << endl; }