相同代码 java8过 java21不过
查看原帖
相同代码 java8过 java21不过
911155
zazhiii楼主2024/8/8 10:37

一样的代码 选java8提交通过,java21 MLE,谁能解释一下

import java.io.*;
import java.util.*;

public class Main {
    static Scanner sc = new Scanner(System.in);
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int n, m, a[][];
    public static void main(String[] args) throws IOException {     
        n = rd.nextInt();
        m = rd.nextInt();
        a = new int[n][m];
        for(int i = 0; i < n; i ++)
            for(int j = 0; j < m; j ++)
                a[i][j] = rd.nextInt();
        int l = 0, r = 1010, ans = r;
        while(l <= r){
            int mid = (l + r) >>> 1;
            if(check(mid)){ans = mid; r = mid - 1;}
            else l = mid + 1;
        }
        pw.println(ans);
        pw.flush();pw.close();
    }
    static int[] dx = {1, -1, 0, 0}, dy = {0, 0, 1, -1};
    static Queue<int[]> que;
    static boolean vis[][];
    public static boolean check(int mid){
        que = new LinkedList<>();
        vis = new boolean[n][m];
        return bfs(mid);
    }
    public static boolean bfs(int mid){
        que.add(new int[]{0, 0});
        vis[0][0] = true;
        int p[], x, y;
        while(!que.isEmpty()){
            p = que.poll();
            x = p[0];
            y = p[1];
            for(int i = 0; i < 4; i ++){
                int nx = x + dx[i];
                int ny = y + dy[i];
                if(nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && a[nx][ny] <= mid){
                    que.add(new int[]{nx, ny});
                    vis[nx][ny] = true;
                    if(nx == n - 1) return true;
                }
            }
        }
        return false;
    }
}

class rd{
    static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static int nextInt() throws IOException {st.nextToken();return (int)st.nval;}
}
2024/8/8 10:37
加载中...