import java.util.*;

class Main { static class Point { int x, y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
}

static int[][] a;
static boolean[][] vis;
static int[][] dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
static int n, m;

static boolean check(int mid) {
    vis = new boolean[n + 1][m + 1];
    Queue<Point> q = new LinkedList<>();
    for (int i = 1; i <= m; i++) {
        q.add(new Point(1, i));
        vis[1][i] = true;
    }
    while (!q.isEmpty()) {
        Point h = q.poll();
        if (h.x == n)
            return true;
        for (int i = 0; i < 4; i++) {
            int tx = dir[i][0] + h.x;
            int ty = dir[i][1] + h.y;
            if (tx <= 0 || tx > n || ty <= 0 || ty > m || vis[tx][ty] || a[tx][ty] > mid)
                continue;
            q.add(new Point(tx, ty));
            vis[tx][ty] = true;
        }
    }
    return false;
}

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    n = scanner.nextInt();
    m = scanner.nextInt();
    a = new int[n + 1][m + 1];
    int l = 1, r = 0, ans = 1010;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            a[i][j] = scanner.nextInt();
            r = Math.max(r, a[i][j]);
        }
    }
    while (l <= r) {
        int mid = (l + r) >> 1;
        if (check(mid))
            r = mid - 1;
        else
            l = mid + 1;
    }
    System.out.println(ans);
}
请用java语言转换下面的c++代码:#includebitsstdc++husing namespace std;int a10101010;bool vis10101010;int dir42=10-10010-1;struct point	int xy;	point	pointint xint yxxyy;int nm;bool checkint mid	memsetvis0sizeofvi

原文地址: https://www.cveoy.top/t/topic/iF17 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录