第二个点超时求助

P1434 [SHOI2002] 滑雪

BZHZS @ 2021-07-02 22:41:21

#include <bits/stdc++.h>
#define ll long long
#define yes {puts("Yes");return 0;}
#define no {puts("No");return 0;}
#define BZHZS puts("BZH is a handsome boy.");
using namespace std;

int n, m, ans;
int walk1[] = {1, -1, 0, 0};
int walk2[] = {0, 0, 1, -1};
int a[103][103], rmb[103][103];

void ts (int a, string tt)
{
    cout << tt << ":: " << a << endl;
    system ("pause");
    return;
}

void dfs (int x, int y, int cur)
{
    if (cur < rmb[x][y])
        return;
    rmb[x][y] = cur;
    for (register int i=0; i<=3; i++)
    {
        int tox = x+walk1[i];
        int toy = y+walk2[i];
        if (tox>=1 && tox<=n && toy>=1 && toy<=m && a[tox][toy]<a[x][y])
            dfs (tox, toy, cur+1);
    }
}

int main()
{
    ios:: sync_with_stdio (0);
    //freopen ("BZHZS.in" , "r" , stdin);
    //freopen ("BZHZS.out " , "w" , stdout);

    cin >> n >> m;
    for (register int i=1; i<=n; i++)
        for (register int j=1; j<=m; j++)
            cin >> a[i][j];

    for (register int i=1; i<=n; i++)
        for (register int j=1; j<=m; j++)
            if (rmb[i][j] == 0)
                dfs (i, j, 0);

    for (register int i=1; i<=n; i++)
        for (register int j=1; j<=m; j++)
            ans = max (ans, rmb[i][j]);

    cout << ans+1 << endl;

    return 0;
}

by _l_l_l_l_l_ @ 2021-07-10 12:33:11

这是我的:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int a[105][105],r,c,ddd[105][105],ans=-1e9;
int dfs(int x,int y)//x,y 以(x,y)结尾的最长滑坡长度
{
    if(ddd[x][y]!=0)return ddd[x][y];
    int return_value=1;//全接不上的话就是1
    if(a[x-1][y]>a[x][y])return_value=max(return_value,dfs(x-1,y)+1);
    if(a[x][y-1]>a[x][y])return_value=max(return_value,dfs(x,y-1)+1);
    if(a[x+1][y]>a[x][y])return_value=max(return_value,dfs(x+1,y)+1);
    if(a[x][y+1]>a[x][y])return_value=max(return_value,dfs(x,y+1)+1);
    ddd[x][y]=return_value;
    return return_value;
}
int main()
{
    scanf("%d%d",&r,&c);
    for(int i=1;i<=r;i++)for(int j=1;j<=c;j++)scanf("%d",&a[i][j]);
    for(int i=1;i<=r;i++)for(int j=1;j<=c;j++)ans=max(ans,dfs(i,j));
    printf("%d\n",ans);
    return 0;
}

|