无法正常造出输出。
#include <bits/stdc++.h>
using namespace std;
const string NAME = "U580678"; //题目名称
//打开文件
void data_start(string s) {
string tres = "data/" + NAME + s, resin = tres + ".in", resout = tres + ".out";
freopen(resin.c_str(), "r", stdin);
freopen(resout.c_str(), "w", stdout);
}
//给出完成提示
void data_end(string s) {
string res = NAME + s;
freopen("CON", "w", stdout);
printf("数据点 %s 已生成完成!\n", res.c_str());
}
const int N = 1e3 + 10;
int a[N][N];bool vis[N][N];
int dx[6] = {0, 0, 0, 1, -1};
int dy[6] = {0, 1, -1, 0, 0};
int n, maxn = -1e9, minn = 1e9;bool can;
void check(int x, int y, int h) {
vis[x][y] = 1;
if(x == n && y == n) {
can = 1;
return;
}
for (int i = 1; i <= 4; i ++) {
int tx = x + dx[i], ty = y + dy[i];
if(!vis[tx][ty] && tx >= 1 && tx <= n && ty >= 1 && ty <= n && abs(a[tx][ty] - a[x][y]) <= h) {
check(tx, ty, h);
}
}
}
void solve() {
scanf("%d", n);
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
scanf("%d", a[i][j]);
minn = min(minn, a[i][j]);
maxn = max(maxn, a[i][j]);
}
}
int l = minn, r = maxn;
while(l <= r) {
int mid = (l + r) / 2;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
vis[i][j] = 0;
}
}
can = 0;check(1, 1, mid);
if(can)
r = mid - 1;
else
l = mid + 1;
}
printf("%d", l);
}
signed main() {
for(int Ti = 1; Ti <= 10; Ti++) {
data_start(to_string(Ti));
solve();
data_end(to_string(Ti));
}
return 0;
}