UVA10285 二维 LIS
2016-08-17 21:02
260 查看
题意: 求二维数组最长不下降子序列
解题:
记忆化搜索, d[i][j]为以 (i, j) 为终点的最长不下降序列的长度,
然后四个方向 dfs ~
解题:
记忆化搜索, d[i][j]为以 (i, j) 为终点的最长不下降序列的长度,
然后四个方向 dfs ~
#include <bits/stdc++.h> #define ll long long using namespace std; const int maxn = 110; const int INF = 0x3f3f3f3f; char nm[maxn]; int a[maxn][maxn], d[maxn][maxn]; int gx[4] = {1,0,-1,0}, gy[4] = {0,1,0,-1}; int t, r, c; int dfs(int x, int y) { if (d[x][y] != -1) return d[x][y]; d[x][y] = 1; for (int i = 0; i < 4; i ++) { int xx = x + gx[i]; int yy = y + gy[i]; if( xx >= 1 && xx <= r && yy >=1 && yy <= c && a[x][y] > a[xx][yy] ) d[x][y] = max (d[x][y], dfs(xx, yy )+1 ); } return d[x][y]; } int main() { scanf ("%d",&t); getchar(); while (t --){ scanf ("%s%d%d",nm, &r,&c); // 行列 for (int i = 1; i <= r; i ++) { for (int j = 1; j <= c; j ++) { scanf ("%d", &a[i][j]); } } int ans = 0; memset (d, -1, sizeof(d)); // solve for (int i= 1; i <= r; i ++) { for (int j = 1; j <= c; j ++) { ans = max (ans, dfs (i, j) ); } } printf ("%s: %d\n",nm, ans); } return 0; }
相关文章推荐
- UVa 10285 Longest Run on a Snowboard(DP 二维最长递减子序列)
- uva 10534 Wavio Sequence LIS 栈实现nlogn
- uva 10534 Wavio Sequence (LIS)
- UVa10534 - Wavio Sequence(LIS)
- hdu----(1677)Nested Dolls(DP/LIS(二维))
- UVA - 10635 Prince and Princess 转化为 LIS
- POJ 3279 + UVA 11464 (二维翻转水题)
- UVA 10652 - Board Wrapping(二维凸包)
- Uva 11019 Matrix Matcher(二维字符串匹配)
- UVA - 10635 Prince and Princess LCS转LIS
- uva 10285 Longest Run on a Snowboard(记忆化搜索)
- UVALive - 6893 The Big Painting (BKDRhash扩展到二维矩阵)
- UVa 12304 (6个二维几何问题合集) 2D Geometry 110 in 1!
- 二维线段树区间修改(add,set)uva11992
- UVa 10534 Wavio Sequence (LIS、LDS)
- 20161026的考试】KMP,二维LIS,DAG删一个点求最长路径最小值(BZOJ 3832)
- UVa 10635 - Prince and Princess(LCS转LIS)
- UVA - 10635 Prince and Princess LCS转LIS
- UVa 10285 BFS的做法
- UVA 11019 二维匹配 AC自动机