第二个点TLE,求助

P1434 [SHOI2002] 滑雪

legend_life @ 2018-11-10 17:15:38

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

const int dx[5] = {1, -1, 0, 0};
const int dy[5] = {0, 0, 1, -1};
int snow[105][105];
int ans, cnt, n, m;

void dfs (int x, int y)
{
    int flag = 0;
    if (cnt > ans)
        ans = cnt;
    for (int i = 0; i < 4; ++ i)
    {
        int xx = x + dx[i];
        int yy = y + dy[i];
        if (xx < 0 || xx >= n || yy < 0 || yy >= m)
        {
            ++ flag;
            continue;
        }
        if (snow[xx][yy] >= snow[x][y])
            ++ flag;
        else
        {
            ++ cnt;
            dfs (xx, yy);
            -- cnt;
        }
    }
}

int main()
{
    scanf ("%d%d", &n, &m);
    for (int i = 0; i < n; ++ i)
        for (int j = 0; j < m; ++ j)
            scanf ("%d", &snow[i][j]);
    for (int i = 0; i < n; ++ i)
        for (int j = 0; j < m; ++ j)
            dfs (i, j);
    printf ("%d\n", ans + 1);
    return 0;
}

|