#include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; int directx[4] = { 0, 1, 0, -1 }; int directy[4] = { -1, 0, 1, 0 }; int res = 0; void dfs(vector<vector<int>>& mat, int sum, int x, int y) { int flag = 0; for (int i = 0; i < 4; i++) { int xx = x + directx[i]; int yy = y + directy[i]; if (xx < mat[0].size() && xx >= 0 && yy < mat.size() && yy >= 0) { if (mat[y][x] < mat[yy][xx]) { flag = 1; break; } } } if (flag == 0) { res = max(res, sum); return; } for (int i = 0; i < 4; i++) { int xx = x + directx[i]; int yy = y + directy[i]; if (xx < mat[0].size() && xx >= 0 && yy < mat.size() && yy >= 0) { if (mat[y][x] < mat[yy][xx]) { dfs(mat, sum + 1, xx, yy); } } } } int main() { int n, m; //n行m列 while (cin >> n >> m) { res = 0; vector<vector<int>> mat(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cin >> mat[i][j]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) dfs(mat, 0, j, i); //每个点都选为起点尝试 } cout << res << endl; } } |
Double click to view unformatted code.