This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// IOI 2021
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ends ' '
#define die(x) return cout << x << endl, 0
#define all(v) v.begin(), v.end()
#define sz(x) (int)(x.size())
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << ends << H; debug_out(T...); }
#define debug(...) cerr << "{" << #__VA_ARGS__ << "}:", debug_out(__VA_ARGS__)
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
////////////////////////////////////////////////////////////////////
const int N = 1e3 + 2;
bool A[N][N];
int W[N][N], D[N][N];
pii X[4][N][N];
int DX[] = {+1, -1, 0, 0};
int DY[] = {0, 0, +1, -1};
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
mt19937 Rnd(time(0));
memset(W, 63, sizeof W), memset(D, 63, sizeof D);
int n, m; cin >> n >> m;
pii src, dest;
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) {
char c; cin >> c;
A[i][j] = (c != '#');
if (c == 'S') src = make_pair(i, j);
if (c == 'C') dest = make_pair(i, j);
}
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (A[i][j]) {
X[0][i][j] = (A[i][j - 1] ? X[0][i][j - 1] : make_pair(i, j));
X[1][i][j] = (A[i - 1][j] ? X[1][i - 1][j] : make_pair(i, j));
}
for (int i = n; i >= 1; i--) for (int j = m; j >= 1; j--) if (A[i][j]) {
X[2][i][j] = (A[i][j + 1] ? X[2][i][j + 1] : make_pair(i, j));
X[3][i][j] = (A[i + 1][j] ? X[3][i + 1][j] : make_pair(i, j));
}
queue<pii> Q;
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (A[i][j] && !(A[i - 1][j] && A[i + 1][j] && A[i][j - 1] && A[i][j + 1])) W[i][j] = 0, Q.push(make_pair(i, j));
while (!Q.empty()) {
pii p = Q.front(); Q.pop();
for (int i = 0; i < 4; i++) {
pii q = make_pair(p.first + DX[i], p.second + DY[i]);
if (A[q.first][q.second] && W[p.first][p.second] + 1 < W[q.first][q.second])
W[q.first][q.second] = W[p.first][p.second] + 1, Q.push(q);
}
}
set< pair<int, pii> > S;
D[src.first][src.second] = 0;
S.insert(make_pair(0, src));
while (!S.empty()) {
pii p = S.begin()->second; S.erase(S.begin());
for (int i = 0; i < 4; i++) {
pii q = make_pair(p.first + DX[i], p.second + DY[i]);
if (A[q.first][q.second] && D[p.first][p.second] + 1 < D[q.first][q.second])
S.erase(make_pair(D[q.first][q.second], q)), D[q.first][q.second] = D[p.first][p.second] + 1, S.insert(make_pair(D[q.first][q.second], q));
q = X[i][p.first][p.second];
if (A[q.first][q.second] && D[p.first][p.second] + W[p.first][p.second] + 1 < D[q.first][q.second])
S.erase(make_pair(D[q.first][q.second], q)), D[q.first][q.second] = D[p.first][p.second] + W[p.first][p.second] + 1, S.insert(make_pair(D[q.first][q.second], q));
}
}
cout << D[dest.first][dest.second] << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |