poj 1088 滑雪

滑雪
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 60769   Accepted: 22147

Description

Michael 喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个 区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
 1  2  3  4 5

16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。

Input

输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。

Output

输出最长区域的长度。

Sample Input

5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

Sample Output

25

Source

 

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
//点的个数 100*100=10000个
//整个区域
int map[101][101];
//用来标记该点是否到过
int mark[101][101];
//方向坐标,上 下 左 右
int dir[4][2] = {-1,0,1,0,0,-1,0,1};
//区域的行数和列数
int row,column;
//输入
void put_in()
{
    cin>>row>>column;
    for(int i = 1; i <= row; ++i)
    for(int j = 1; j <= column; ++j)
    cin>>map[i][j];
}

int dfs(int px,int py)
{
    //记录这一点到最后所用的步子
    int steps;
    //记录这一点到最后所用的步子,当有新的方向比目前的步子长,就更新
    int max_step = 0;
    //首先判断该点以前是否有计算过,若计算过则直接可以使用
    if(mark[px][py]!=0)
    return mark[px][py];
    //从上下左右四个方向进行递归运算
    //1、上方
    for(int i = 0; i < 4; i++)
    {
        int x,y;
        x = px + dir[i][0];
        y = py + dir[i][1];
        //判断边界
        if(x<1||x>row||y<1||y>column)
        continue;
        if(map[px][py]>map[x][y])
        {
            //可以进入
            steps = dfs(x,y)+1;
            //如果比原来的有更长的步子,就更新他
            if(steps > max_step)
            max_step = steps;
        }
    }
    //这里的赋值是为了以后再次查询到这里时,可以直接使用而不必再次计算
    return  mark[px][py] = max_step;
}

int main()
{
    int tem,maxtem=0;
    put_in();
    memset(mark,sizeof(mark),0);
    for(int i = 1;i <= row ; i++)
    for(int j = 1;j <= column ;j++)
    {
        tem = dfs(i,j);
        if(tem>maxtem)
        maxtem = tem;
    }
    cout<<maxtem+1<<endl;
}

测试数据及答案:

5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9


1 1
1

1 2
1 2

1 3 
1 3 2

2 2
1 2 
4 3

2 2 
1 2 
3 4

4 7
7 6 5 4 3 2 1
1 5 1 1 1 1 1
1 4 3 1 1 1 1
1 5 6 7 8 1 1

3 3
9 1 2
5 6 7
8 4 3

3 4
1 2 3 4
8 7 6 5
9 10 11 12

3 3
0 0 0
0 5 0
0 0 0

12 13
1   1 30  4  800  6  7  8  99 10 1223 1
20 30 30 4 16 15 14 13 12 11 1
21 22 99 444444 88 9926 27 9928 9929 3000 456 1
40 39 1 90 36 35 34 33 3992 30001 789  1
41 42 4000 44  88 46 47 48 49 50 897  1
1 59 1 57 56  85 54 53 52 51 908 1
61 77 56 64 444 66 67 68 69 70 1234 1
80 79 78 77 76 75 74 73 72 71 12345 1
81 82 2  2 4 86 5 88 8 90 3456  1
100 99 98 97 96 95 94 93 92 91 567 1
890 654 623 154 683 15414 86549 633 123 456 123456  1
9517 45632 643164 3478643 43 16 431 64453132 689431 746546 15643 1
64543 13146543 13474 314789 4352154 65431 631 654324 65132 89547  34567312 1 1


13 12
1   1 30  4  800  6  7  8  99 10 1223 1
20 30 30 4 16 15 14 13 12 11 1
21 22 99 444444 88 9926 27 9928 9929 3000 456 1
40 39 1 90 36 35 34 33 3992 30001 789  1
41 42 4000 44  88 46 47 48 49 50 897  1
1 59 1 57 56  85 54 53 52 51 908 1
61 77 56 64 444 66 67 68 69 70 1234 1
80 79 78 77 76 75 74 73 72 71 12345 1
81 82 2  2 4 86 5 88 8 90 3456  1
100 99 98 97 96 95 94 93 92 91 567 1
890 654 623 154 683 15414 86549 633 123 456 123456  1
9517 45632 643164 3478643 43 16 431 64453132 689431 746546 15643 1
64543 13146543 13474 314789 4352154 65431 631 654324 65132 89547  34567312 1 1

3 3
0 1 2
1 0 1
2 1 0

3 3
0 0 0
0 0 0
0 0 0

1 1
0

10 10
1 2 300 4 5 6 7 8 9 10
20 19 18 17 16 15 14 13 12 11
21 22 23 24 25 26 27 28 29 30
40 39 38 37 36 35 34 33 32 31
41 42 43 44 45 46 47 48 49 50
60 59 58 57 56 55 54 53 52 51
61 62 63 64 65 66 67 68 69 70
80 79 78 77 76 75 74 73 72 71
81 82 83 84 85 86 87 88 89 90
100 99 98 97 96 95 94 93 92 91

4 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

4 4
1 2 2 1
1 4 4 1
1 3 3 1
1 2 2 1

3 3
9 1 2
5 6 7
8 4 3

/////////////////////////////////////////////////////////////////////////////

25
1
2
2
4
3
7
4
12
2
27
37
3
1
1
97
4
4
4
原文地址:https://www.cnblogs.com/newpanderking/p/2670831.html