#include #include #include using namespace std; vector> dir = {{0,1},{1,0},{0,-1},{-1,0}}; int main() { int R, C, K; cin >> R >> C >> K; vector> grid(R, vector(C)); for (int i=0; i> grid[i][j]; // Build graph vector>> neigh(R*C*(K+1)+2); for (int k=0; k<=K; k++) { for (int i=0; i=R || j+dir[d].second < 0 || j+dir[d].second >= C) continue; // In-plane transitions neigh[k*R*C+i*C+j].push_back({k*R*C+(i+dir[d].first)*C+(j+dir[d].second),grid[i+dir[d].first][j+dir[d].second]}); // Out-of-plane transitions if (k != K) neigh[k*R*C+i*C+j].push_back({(k+1)*R*C+(i+dir[d].first)*C+(j+dir[d].second),1000000000}); } } } } // Entry into bottom row for (int j=0; j done(R*C*(K+1)+2, false); vector dist(R*C*(K+1)+2, 0); dist[R*C*(K+1)]=1000000000; priority_queue> work; work.push({1000000000,R*C*(K+1)}); while (!work.empty()) { auto top = work.top(); work.pop(); if (done[top.second]) continue; done[top.second] = true; for (auto t : neigh[top.second]) { if (done[t.first]) continue; if (dist[t.first] >= min(t.second, dist[top.second])) continue; dist[t.first] = min(t.second, dist[top.second]); work.push({dist[t.first],t.first}); } } cout << dist[R*C*(K+1)+1] << endl; return 0; }