2025-09-12 14:50:25 -03:00

183 lines
3.8 KiB
C++

/* Problem URL: https://codeforces.com/gym/104020/problem/K */
#include <bits/stdc++.h>
using namespace std;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (size_t i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
set<int> attempts;
for (int i = 1; i <= n * m; i++) {
attempts.insert(i);
}
vvi fds(n, vi(m));
cin >> fds;
auto choice = [&](int i, int j, int t) {
int add[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int ans = INT32_MAX >> 1;
for (auto &k : add) {
int id = i + k[0];
int jd = j + k[1];
if (id >= 0 && id < n && jd >= 0 && jd < m) {
int now = fds[id][jd];
if (abs(t - now) < abs(t - ans) || (abs(t - now) == abs(t - ans) && abs(fds[i][j] - now) < abs(fds[i][j] - ans))) {
ans = now;
}
}
}
return ans;
};
vvvi graph(n * m, vvi(n * m));
vvvi look(n * m, vvi(n * m));
for (int i = 1; i <= n * m; i++) {
rep(j, n) {
rep(k, m) {
if (fds[j][k] == i) {
continue;
}
int tmp = choice(j, k, i);
graph[i - 1][tmp - 1].push_back(fds[j][k] - 1);
look[i - 1][fds[j][k] - 1].push_back(tmp - 1);
}
}
}
vvi depth(n * m, vi(n * m, 0));
V<bool> vis(n * m);
function<void(int, int, int)> dfs = [&](int i, int t, int d) {
vis[i] = true;
depth[i][t] = d;
for (auto j : graph[t][i]) {
if (vis[j]) {
continue;
}
dfs(j, t, d + 1);
}
};
rep(i, n * m) {
fill(all(vis), false);
dfs(i, i, 0);
auto itr = attempts.begin();
while (itr != attempts.end()) {
if (!vis[*itr]) {
itr = attempts.erase(itr);
} else {
itr++;
}
}
if (attempts.empty()) {
break;
}
}
if (attempts.empty()) {
cout << "impossible\n";
return 0;
}
int ans = 0;
int minimal = INT32_MAX >> 1;
for (auto i : attempts) {
int get = 0;
for (auto j : depth[i]) {
rmax(get, j);
}
if (get < minimal) {
ans = i;
minimal = get;
}
}
cout << ans + 1 << ' ' << minimal << '\n';
}