哪里错了!!!(6,8,9,10WA)

P1434 [SHOI2002] 滑雪

LabmemNo_012LzTopic @ 2024-09-10 20:59:37

#include <iostream>
#include<queue>
#include<cmath>
using namespace std;

int n, m, map[105][105] = { 0 };//行,列,地图

int k[105][105] = { 0 }, ans = 0;

int fx[4] = { 1,-1,0,0 }, fy[4] = { 0,0,1,-1 };//方向

int dfs(int x, int y) 
{
    if (k[x][y])return k[x][y];
    k[x][y] = 1;
    int xl, yl;
    for (int i = 0; i < 4; i++)
    {
        xl = fx[i] + x;
        yl = fy[i] + y;
        if (xl<1 || xl>n || yl<1 || yl>m || map[xl][yl] > map[x][y])continue;
        dfs(xl, yl);
        k[x][y] = max(k[x][y], k[xl][yl] + 1);
    }
    return k[x][y];
}

int main()
{
    //输入
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> map[i][j];
        }
    }

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            ans = max(ans, dfs(i, j));
        }
    }
    cout << ans << endl;
    return 0;
}

by Down_syndrome @ 2024-09-10 21:03:30

@LabmemNo_012LzTopic

#include <iostream>
#include<queue>
#include<cmath>
using namespace std;

int n, m, map[105][105] = { 0 };//行,列,地图

int k[105][105] = { 0 }, ans = 0;

int fx[4] = { 1,-1,0,0 }, fy[4] = { 0,0,1,-1 };//方向

int dfs(int x, int y) 
{
    if (k[x][y])return k[x][y];
    k[x][y] = 1;
    int xl, yl;
    for (int i = 0; i < 4; i++)
    {
        xl = fx[i] + x;
        yl = fy[i] + y;
        if (xl<1 || xl>n || yl<1 || yl>m || map[xl][yl] >= map[x][y])continue;//只改了这里
        dfs(xl, yl);
        k[x][y] = max(k[x][y], k[xl][yl] + 1);
    }
    return k[x][y];
}

int main()
{
    //输入
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> map[i][j];
        }
    }

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            ans = max(ans, dfs(i, j));
        }
    }
    cout << ans << endl;
    return 0;
}

by feizhu_QWQ @ 2024-09-10 21:28:14

@LabmemNo_012LzTopic
找到了,你的判断大小是大于等于而不是大于
第21行,dfs判断时


by LabmemNo_012LzTopic @ 2024-09-10 22:09:36

@feizhu0130 感谢!!!


by LabmemNo_012LzTopic @ 2024-09-10 22:09:53

@c20220625 感谢!!!


|