40分WA求助

P1434 [SHOI2002] 滑雪

The_Wandering_Earth @ 2023-03-26 11:51:50

#include<bits/stdc++.h>

using namespace std;

int n, m, g[205][205], f[205][205], ans = 1;
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};

void dfs(int row, int col); 

signed main()
{
    cin >> n >> m;
    memset(f, -1, sizeof(f));
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            cin >> g[i][j];
        }
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            dfs(i, j);
        }
    }
    cout << ans;
} 

void dfs(int row, int col)
{
    if(f[row][col] != -1)
    {
        return;
    }
    f[row][col] = 1;
    for(int i = 0; i < 4; i++)
    {
        int r = row + dr[i], c = col + dc[i];
        if(r < 1 || r > n || c < 1 || c > m)
        {
            continue;
        }
        if(g[r][c] < g[row][col] && f[r][c] != 0)
        {
            dfs(r, c);
        }
        f[row][col] = max(f[row][col], f[r][c] + 1);
    }
    ans = max(ans, f[row][col]);
}

by jiangjiangQwQ @ 2023-03-27 20:06:40

f[row][col] = max(f[row][col], f[r][c] + 1); 这一行要放if里面力,这个点合法才更新答案 @zhangpeinan123


by jiangjiangQwQ @ 2023-03-27 20:07:22

#include<bits/stdc++.h>

using namespace std;

int n, m, g[205][205], f[205][205], ans = 1;
int dr[4] = {-1, 1, 0, 0 };
int dc[4] = { 0, 0, -1, 1 };

void dfs(int row, int col);

signed main() {
    cin >> n >> m;
    memset(f, -1, sizeof(f));
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> g[i][j];
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            dfs(i, j);
        }
    }
    cout << ans;
}

void dfs(int row, int col) {
    if (f[row][col] != -1) {
        return;
    }
    f[row][col] = 1;
    for (int i = 0; i < 4; i++) {
        int r = row + dr[i], c = col + dc[i];
        if (r < 1 || r > n || c < 1 || c > m) {
            continue;
        }
        if (g[r][c] < g[row][col] && f[r][c] != 0) {
            dfs(r, c);
            f[row][col] = max(f[row][col], f[r][c] + 1);
        }
    }
    ans = max(ans, f[row][col]);
}

by The_Wandering_Earth @ 2023-03-27 20:08:02

@jiangshuhong 感激不尽


by The_Wandering_Earth @ 2023-03-27 20:09:20

此题已解决,警钟长鸣


|