PKU POJ 1088 滑雪 搜索

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int r,c;
int map[105][105],height[105][105];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
inline bool judge(int x,int y)
{
    return x>=0&&y>=0&&x<r&&y<c;
}
void dfs(int x,int y)
{
    int i,x1,y1;
    bool f=true;
    for (i=0;i<4;++i)
    {
        x1=x+dir[i][0];
        y1=y+dir[i][1];
        if(judge(x1,y1)&&map[x][y]>map[x1][y1])
        {
            if(height[x1][y1]==0)
                dfs(x1,y1);
            if(height[x1][y1]+1>height[x][y])
                height[x][y]=height[x1][y1]+1;
            f=false;
        }
    }
    if(f)
        height[x][y]=1;
}
int main()
{
    int i,j,m;
    while (scanf("%d%d",&r,&c)!=EOF)
    {
        for (i=0;i<r;++i)
            for (j=0;j<c;++j)
                scanf("%d",&map[i][j]);
        for (i=0;i<r;++i)
            for (j=0;j<c;++j)
                height[i][j]=0;
        for (i=0;i<r;++i)
            for (j=0;j<c;++j)
                if (height[i][j]==0)
                    dfs(i,j);
        m=0;
        for (i=0;i<r;++i)
            for (j=0;j<c;++j)
                if(m<height[i][j])
                    m=height[i][j];
        printf("%d\n",m);
    }
    return 0;
}

 

posted on 2013-03-18 21:53  Deller  阅读(164)  评论(0)    收藏  举报

导航