随便写了个二分,样例输出 0,一交就过了。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e3 + 5;
int n, m, p[N][N], ans;
bool vis[N][N];
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
struct node{
int x, y;
};
bool check (int mid){
queue<node> q;
memset (vis, 0, sizeof(vis));
for (int i = 1; i <= n; ++i){
q.push(node{1, i});
vis[1][i] = 1;
}
while (!q.empty()){
node tmp = q.front(); q.pop();
int x = tmp.x, y = tmp.y;
if (x == n) return 1;
for (int i = 0; i < 4; ++i){
int xx = x + dx[i], yy = y + dy[i];
if (xx < 1 || xx > n || yy < 1 || yy > n || vis[xx][yy] || p[xx][yy] > mid) continue;
vis[xx][yy] = 1;
q.push(node{xx, yy});
if (xx == n) return 1;
}
}
return 0;
}
signed main (){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
int l = LONG_LONG_MAX, r = -1;
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
cin >> p[i][j];
l = min (l, p[i][j]);
r = max (r, p[i][j]);
}
}
while (l <= r){
int mid = l + r >> 1;
if (check(mid)){
ans = mid;
r = mid - 1;
}else l = mid + 1;
}
cout << ans << '\n';
return 0;
}