Advertisement
Guest User

Untitled

a guest
Jan 18th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <cmath>
  11. #include <queue>
  12. #include <stack>
  13. #include <deque>
  14. #include <cassert>
  15. #include <ctime>
  16. #include <climits>
  17. using namespace std;
  18.  
  19. #ifdef WIN32
  20. #define LLD "%I64d"
  21. #else
  22. #define LLD "%lld"
  23. #endif
  24.  
  25. typedef long long ll;
  26. typedef long double ld;
  27.  
  28. #define mp make_pair
  29. #define pb push_back
  30. #define sz(x) (int)(x).size()
  31. #define X first
  32. #define Y second
  33. #define all(x) x.begin(),x.end()
  34. #define y1 y11111
  35.  
  36. const int maxn = 1000 + 10;
  37. int a[maxn][maxn], dp[maxn][maxn];
  38. bool used[maxn][maxn];
  39. vector< pair< int, pair<int, int> > > v;
  40. pair<int, int> turn[4] = { mp(0, 1), mp(1, 0), mp(-1, 0), mp(0, -1) };
  41.  
  42. int n, m;
  43. bool correct(int x, int y) {
  44. return (x >= 0 && y >= 0 && x < n && y < m);
  45. }
  46.  
  47. void dfs(int x, int y) {
  48. used[x][y] = 1;
  49. for (int i = 0; i < 4; i++) {
  50. int newx = x + turn[i].X;
  51. int newy = y + turn[i].Y;
  52. if (correct(newx, newy) && a[x][y] + 1 == a[newx][newy]) {
  53. dp[newx][newy] = max(dp[newx][newy], dp[x][y] + 1);
  54. if (!used[newx][newy])
  55. dfs(newx, newy);
  56. }
  57. }
  58. }
  59.  
  60. int main() {
  61. #ifdef _DEBUG
  62. freopen("input.txt", "r", stdin);
  63. freopen("output.txt", "w", stdout);
  64. #else
  65. freopen("sherlocked.in", "r", stdin);
  66. freopen("sherlocked.out", "w", stdout);
  67. #endif
  68.  
  69. scanf("%d%d", &n, &m);
  70. for (int i = 0; i < n; i++) {
  71. for (int j = 0; j < m; j++) {
  72. scanf("%d", &a[i][j]);
  73. v.pb(mp(a[i][j], mp(i, j)));
  74. }
  75. }
  76.  
  77. sort(all(v));
  78. //reverse(all(v));
  79.  
  80. for (int i = 0; i < sz(v); i++) {
  81. if (!used[v[i].Y.X][v[i].Y.Y]) {
  82. dfs(v[i].Y.X, v[i].Y.Y);
  83. }
  84. }
  85.  
  86. int ans = 0;
  87. for (int i = 0; i < n; i++) {
  88. for (int j = 0; j < m; j++) {
  89. ans = max(ans, dp[i][j]);
  90. }
  91. }
  92.  
  93. printf("%d", ans + 1);
  94.  
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement