一模一样的代码,C++能过,JAVA确RE在第二个点,我真的不知道哪里错了

P1434 [SHOI2002] 滑雪

Wuwuxiaocai @ 2022-11-04 00:18:08

import java.util.Scanner;

public class Main {

    static long[][] map = new long[310][310];
    static int N, M;
    static long ans = 0;
    static int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    static long[][] arr = new long[310][310];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        N = in.nextInt();
        M = in.nextInt();
        for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) map[i][j] = in.nextInt();
        for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) ans = Math.max(ans, dfs(i, j));
        System.out.print(ans);
    }

    static long dfs(int x, int y) {
        if (arr[x][y] != 0) return arr[x][y];
        long temp = 1;
        long ans = 1;
        for (int[] dir : dirs) {
            int xx = x + dir[0];
            int yy = y + dir[1];
            if (xx < 0 || xx >= N || yy < 0 || yy >= M || map[xx][yy] <= map[x][y]) continue;
            ans = Math.max(ans, temp + dfs(xx, yy));
        }
        arr[x][y] = ans;
        return ans;
    }
}
#include "bits/stdc++.h"
using namespace std;
#define LL long long
int dirs[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int N, M;
long long ans = 0;
LL ma[310][310];
LL arr[310][310];

LL dfs(int x, int y) {

    if (arr[x][y] != 0) return arr[x][y];
    long temp = 1; long an = 1;
    for (int i = 0; i < 4; i++)  {
        int xx = x + dirs[i][0];
        int yy = y + dirs[i][1];
        if (xx < 0 || xx >= N || yy < 0 || yy >= M || ma[xx][yy] <= ma[x][y]) continue;
        if (temp + dfs(xx, yy) > an) {
            an = temp + dfs(xx, yy);
        }
    }
    arr[x][y] = an;
    return an;
}

int main() {
    cin >> N >> M;
    for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) cin >> ma[i][j];
    for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) ans = max(ans, dfs(i, j));
    cout << ans;
    return 0;
}

by wxr0108 @ 2022-11-08 17:02:35

话说这题我终于满分了


|