Skip to content

Instantly share code, notes, and snippets.

@vishnupsatish
Created June 6, 2021 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vishnupsatish/a644d9f0f2849b137680d461f7dc9f89 to your computer and use it in GitHub Desktop.
Save vishnupsatish/a644d9f0f2849b137680d461f7dc9f89 to your computer and use it in GitHub Desktop.
#include <iostream>
#define el "\n"
#define ll long long
using namespace std;
const int MM = 1502;
int dp[MM][MM], n;
ll a[MM][MM];
int f(int r, int c) {
if (dp[r][c] != 0) return dp[r][c];
int re = 0;
if (a[r - 1][c] != 0 && a[r - 1][c] > a[r][c]) re = max(re, 1 + f(r - 1, c));
if (a[r + 1][c] != 0 && a[r + 1][c] > a[r][c]) re = max(re, 1 + f(r + 1, c));
if (a[r][c - 1] != 0 && a[r][c - 1] > a[r][c]) re = max(re, 1 + f(r, c - 1));
if (a[r][c + 1] != 0 && a[r][c + 1] > a[r][c]) re = max(re, 1 + f(r, c + 1));
return dp[r][c] = re;
}
int main() {
cin.tie(0); ios::sync_with_stdio(0);
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
ans = max(ans, f(i, j));
}
}
cout << ans << el;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment