#include #include using namespace std; constexpr int DX[] = {0, 1, 0, -1}; constexpr int DY[] = {1, 0, -1, 0}; int path_length = 0; void log_grid(const vector>& grid, string name) { for (const auto& row : grid) { clog << name << ": "; for (const auto& cell : row) { clog << cell << " "; } clog << endl; } } int dfs(const vector>& grid, const int x, const int y, vector>& calculated_depths, const int _depth = 1) { const unsigned int r = grid.size(), c = grid[0].size(); const int current_num = grid[x][y]; if (calculated_depths[x][y] > 0) { return calculated_depths[x][y] + _depth - 1; } if (_depth + current_num <= path_length) { // clog << "CUT " << x << "," << y << ":" << current_num << ", " << _depth << endl; return -1; } // 剪枝 // if (current_num == 1) { return _depth; } // clog << x << "," << y << ":" << current_num << ", " << _depth << endl; int best_depth = _depth; for (int i = 0; i < 4; i++) { const int nx = x + DX[i], ny = y + DY[i]; if (nx >= 0 && nx < r && ny >= 0 && ny < c && grid[nx][ny] < current_num) { int next_depth = dfs(grid, nx, ny, calculated_depths, _depth + 1); best_depth = max(best_depth, next_depth); } } path_length = max(path_length, best_depth); calculated_depths[x][y] = best_depth - _depth + 1; return best_depth; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int r, c; cin >> r >> c; vector> grid(r, vector(c)); vector> calculated_depths(r, vector(c, 0)); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { cin >> grid[i][j]; } } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { dfs(grid, i, j, calculated_depths); // clog << "---" << endl; // log_grid(calculated_depths, "calculated_depths"); // clog << "---" << endl; } } cout << path_length << endl; return 0; }