tyvj1004 滑雪

July 6, 2013
tyvj

题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1004 记忆化搜索


#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;
int m[102][102], dp[102][102], r, C;
int dfs(int x, int y) {
    if (dp[x][y] > 0) return dp[x][y]; dp[x][y]=1; int a,b,c,d; a=b=c=d=0;
    if (x-1>=1 && m[x-1][y] < m[x][y]) a = dfs(x-1,y);
    if (x+1<=r && m[x+1][y] < m[x][y]) b = dfs(x+1,y);
    if (y-1>=1 && m[x][y-1] < m[x][y]) c = dfs(x,y-1);
    if (y+1<=C && m[x][y+1] < m[x][y]) d = dfs(x,y+1);
    dp[x][y] = max(max(a,b),max(c,d))+1; return dp[x][y];
}
int main(void) {
    int Max;
    freopen("in.txt","r",stdin);
    scanf("%d%d",&r, &C);
    for (int i = 1; i <= r; ++i) for (int j = 1; j <= C; ++j) scanf("%d", &m[i][j]), dp[i][j] = 0; Max=1; 
    for (int i = 1; i <= r; ++i) for (int j = 1; j <= C; ++j) {dp[i][j] = dfs(i, j); if (dp[i][j]>Max) Max=dp[i][j];}
    for (int i = 1; i <= r; ++i) {
        for (int j = 1; j <= C; ++j) printf("%d ", dp[i][j]); printf("\n");
    }
    printf("%d\n", Max);
    
    return 0;
}

因为全局变量和局部变量冲突错了很久。

comments powered by Disqus