Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <cstdio>
- #include <cstdlib>
- #include <string>
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <set>
- #include <map>
- #include <cmath>
- #include <queue>
- #include <stack>
- #include <deque>
- #include <cassert>
- #include <ctime>
- #include <climits>
- using namespace std;
- #ifdef WIN32
- #define LLD "%I64d"
- #else
- #define LLD "%lld"
- #endif
- typedef long long ll;
- typedef long double ld;
- #define mp make_pair
- #define pb push_back
- #define sz(x) (int)(x).size()
- #define X first
- #define Y second
- #define all(x) x.begin(),x.end()
- #define y1 y11111
- const int maxn = 1000 + 10;
- int a[maxn][maxn], dp[maxn][maxn];
- bool used[maxn][maxn];
- vector< pair< int, pair<int, int> > > v;
- pair<int, int> turn[4] = { mp(0, 1), mp(1, 0), mp(-1, 0), mp(0, -1) };
- int n, m;
- bool correct(int x, int y) {
- return (x >= 0 && y >= 0 && x < n && y < m);
- }
- void dfs(int x, int y) {
- used[x][y] = 1;
- for (int i = 0; i < 4; i++) {
- int newx = x + turn[i].X;
- int newy = y + turn[i].Y;
- if (correct(newx, newy) && a[x][y] + 1 == a[newx][newy]) {
- dp[newx][newy] = max(dp[newx][newy], dp[x][y] + 1);
- if (!used[newx][newy])
- dfs(newx, newy);
- }
- }
- }
- int main() {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #else
- freopen("sherlocked.in", "r", stdin);
- freopen("sherlocked.out", "w", stdout);
- #endif
- scanf("%d%d", &n, &m);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- scanf("%d", &a[i][j]);
- v.pb(mp(a[i][j], mp(i, j)));
- }
- }
- sort(all(v));
- //reverse(all(v));
- for (int i = 0; i < sz(v); i++) {
- if (!used[v[i].Y.X][v[i].Y.Y]) {
- dfs(v[i].Y.X, v[i].Y.Y);
- }
- }
- int ans = 0;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- ans = max(ans, dp[i][j]);
- }
- }
- printf("%d", ans + 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement