poj1088

//

//  main.cpp

//  poj1088

//

//  Created by Jasmine wang on 05/09/2017.

//  Copyright © 2017 Jasmine wang. All rights reserved.

//

这道题是典型的递归 事实上我用的递推。郭老师给出了两种方法,但首先都是确定长度函数l[][]

1、人人为我:即从某一点开始,若小于临近数值,则临近点的的l加一 若非,则临近点l是1 上下左右都做判断

2、我为人人:从一点开始,若大于临近数值,则该点是临近点+1,然后移到该临近点;若非,则该点为1

#include <iostream>

#include <algorithm>

#include <cstring>

using namespace std;

#define MAXN 105

int R, C;

int h[MAXN][MAXN], l[MAXN][MAXN];

int longestpath(int x, int y){

    //int left = 0,right = 0, up = 0, down = 0, m;

    if(l[x][y] > 0){

        return l[x][y];

    }

    if(x + 1 <= R){

        if(h[x][y] < h[x + 1][y]){

            h[x + 1][y] = max(h[x + 1][y], h[x][y] + 1);

        }

        else h[x + 1][y] = 1;

    }

    else h[x + 1][y] = 1;

    if(x - 1 >= 1){

        if(h[x][y] < h[x - 1][y]){

            h[x - 1][y] = max(h[x - 1][y], h[x][y] + 1);

        }

        else h[x - 1][y] = 1;

    }

    else h[x - 1][y] = 1;

    if(y + 1 <= C){

        if(h[x][y] < h[x][y + 1]){

            h[x][y + 1] = max(h[x][y + 1], h[x][y] + 1);

        }

        else h[x][y + 1] = 1;

    }

    else h[x][y + 1] = 1;

    if(y - 1 >= 1){

        if(h[x][y] < h[x][y - 1]){

            h[x][y - 1] = max(h[x][y - 1], h[x][y] + 1);

        }

        else h[x][y - 1] = 1;

    }

    else h[x][y - 1] = 1;

    //m = max(left, max(right, max(up, down)));

     

   /*

    if(x+1<=R){

        if(h[x+1][y]<h[x][y])

            up=1+longestpath(x+1,y);

        else up=1;

    }

    else up=1;

    if(x-1>=1){

        if(h[x-1][y]<h[x][y])

            down=1+longestpath(x-1,y);

        else down=1;

    }

    else down=1;

    if(y+1<=C){

        if(h[x][y+1]<h[x][y])

            right=1+longestpath(x,y+1);

        else right=1;

    }

    else right=1;

    if(y-1>=1){

        if(h[x][y-1]<h[x][y])left=1+longestpath(x,y-1);

        else left=1;

    }

    else left=1;

    m = max(left, max(right, max(up, down)));*/

    return h[x][y];

}

int main(int argc, const char * argv[]) {

    int i, j, ans = 0;

    scanf("%d%d", &R, &C);

    for( i = 1; i <= R; i ++){

        for(j = 1; j <= C; j ++){

            scanf("%d", &h[i][j]);

        }

    }

    memset(l,0,sizeof(l));

    for(i = 1; i <= R; i ++){

        for( j = 1; j <= C; j ++){

            l[i][j] = longestpath(i, j);

            if(ans < l[i][j]){

                ans = l[i][j];

            }

        }

    }

    printf("%d", ans);

    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值