poj1088

#include    <iostream>
#include    <cstring>
#include    <cstdlib>

#define SIZE 101
using namespace std;

int N,M;
int maze[SIZE][SIZE];
int opt[SIZE][SIZE];
bool visit[SIZE][SIZE];
int result;

int direction[4][2]={
    0,1,
    1,0,
    0,-1,
    -1,0
};
void init()
{
    memset(opt,0,sizeof(opt));
    memset(visit,0,sizeof(visit));
}
int max(int x,int y)
{
    return x>y?x:y;
}

bool isvalid(int x, int y){

    if(x<0||y<0||x>=N||y>=M)
        return false;
    return true;
}

void Input ()
{
    result = 0;
    init();
    scanf("%d %d",&N,&M);
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            scanf("%d",&maze[i][j]);
        }
    }

}        /* -----  end of function Input  ----- */

void dfs(int x,int y)
{    
    //if(opt[x][y]>0) return;
    if(visit[x][y])
        return;

    visit[x][y]=true; //forbid searched again:dead loop!

    for(int i=0;i<4;i++){
        int tx,ty;
        tx = x + direction[i][0];
        ty = y + direction[i][1];
        if(!isvalid(tx,ty)) continue;
        dfs(tx,ty);
        if(maze[tx][ty]<maze[x][y]){
            opt[x][y]=max(opt[tx][ty]+1,opt[x][y]);
        }
    }

    visit[x][y] = false;//allow backtrace!other point can reuse it

}//simply dfs


void Solve ()
{
    
    for(int i=0;i<N;i++){
        for(int j=0;j<M;j++){
            //init();
            memset(opt,0,sizeof(opt));
            memset(visit,0,sizeof(visit));
            dfs(i,j);
            result = max(result,opt[i][j]);
        }
    }
    
}        /* -----  end of function Solve  ----- */

void Output ()
{
    printf("%d\n",result+1);

}        /* -----  end of function Output  ----- */

int main ()
{
    Input();
    Solve();
    Output();

    return 0;
}        /* ----------  end of function main  ---------- */


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值