POJ 1088 滑雪——DP

来源:互联网 发布:做网站必备软件 编辑:程序博客网 时间:2024/04/24 06:43

记忆化搜索

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int dx[] = {-1, 1, 0, 0};const int dy[] = {0, 0, -1, 1};int m, n, a[110][110], dp[110][110];int dfs(int x, int y) {    if (dp[x][y] != -1) return dp[x][y];    int ans = 0;    for (int i = 0; i < 4; i++) {        int xx = x + dx[i], yy = y + dy[i];        if (1 <= xx && xx <= m && 1 <= yy && yy <= n && a[xx][yy] < a[x][y]) {            ans = max(ans, dfs(xx, yy));        }    }    return dp[x][y] = ans + 1;}int main(){    while (~scanf("%d %d", &m, &n)) {        for (int i = 1; i <= m; i++) {            for (int j = 1; j <= n; j++) {                scanf("%d", &a[i][j]);            }        }        memset(dp, -1, sizeof(dp));        int ans = 0;        for (int i = 1; i <= m; i++) {            for (int j = 1; j <= n; j++) {                ans = max(ans, dfs(i, j));            }        }        printf("%d\n", ans);    }    return 0;}