1
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
constexpr int DX[] = {0, 1, 0, -1};
|
||||
constexpr int DY[] = {1, 0, -1, 0};
|
||||
|
||||
int longest_path(const vector<vector<int> > &grid, int x = -1, int y = -1, int depth = 0) {
|
||||
const unsigned int r = grid.size(), c = grid[0].size();
|
||||
if (x == -1 && y == -1) {
|
||||
for (int i = 0; i < r; i++) {
|
||||
for (int j = 0; j < c; j++) {
|
||||
if (grid[i][j] == r * c) {
|
||||
x = i, y = j, depth = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
int nx = x + DX[i], ny = y + DY[i];
|
||||
if (nx >= 0 && nx < grid.size() && ny >= 0 && ny < grid[0].size()
|
||||
&& grid[nx][ny] == grid[x][y] - 1) {
|
||||
return longest_path(grid, nx, ny, depth + 1);
|
||||
}
|
||||
}
|
||||
return depth;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int r, c;
|
||||
cin >> r >> c;
|
||||
|
||||
vector<vector<int> > grid(r, vector<int>(c));
|
||||
for (int i = 0; i < r; i++) {
|
||||
for (int j = 0; j < c; j++) {
|
||||
cin >> grid[i][j];
|
||||
}
|
||||
}
|
||||
cout << longest_path(grid) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user