描述
Michael喜歡滑雪百這並不奇怪, 因爲滑雪的確很刺激。可是爲了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待升降機來載你。Michael想知道載一個區域中最長底滑坡。區域由一個二維數組給出。數組的每個數字代表點的高度。下面是一個例子
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
一個人可以從某個點滑向上下左右相鄰四個點之一,當且僅當高度減小。在上面的例子中,一條可滑行的滑坡爲24-17-16-1。當然25-24-23-...-3-2-1更長。事實上,這是最長的一條。
輸入
第一行表示有幾組測試數據,輸入的第二行表示區域的行數R和列數C(1 <= R,C <= 100)。下面是R行,每行有C個整數,代表高度h,0<=h<=10000。
後面是下一組數據;
輸出最長區域的長度。
樣例輸入
1
5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
樣例輸出
25
和POJ1088一樣,可以戳POJ 1088 - 滑雪
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
int a[110][110], f[110][110] = {0};
int m, n;
int MAXN, ans;
int DFS(int x, int y)
{
int ans = 0;
if (f[x][y] != 0)
return f[x][y];
for (int i = 0; i < 4; ++i)
{
int xx = x + dx[i];
int yy = y + dy[i];
if (xx < 1 || xx > n || yy < 1 || yy > m || a[xx][yy] >= a[x][y])
continue;
int t = DFS(xx, yy);
ans = max(ans, t);
}
f[x][y] = ans + 1;
return f[x][y];
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
memset(f, 0, sizeof(f));
MAXN = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
scanf("%d", &a[i][j]);
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
int t;
t = DFS(i, j);
MAXN = max(MAXN, t);
}
}
printf("%d\n", MAXN);
}
return 0;
}