This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
// #define int long long
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define kill(x) cout << x << "\n", exit(0);
#define pii pair<int, int>
#define endl "\n"
using namespace std;
typedef long long ll;
// typedef __int128_t lll;
typedef long double ld;
const int MAXN = (int)1e3 + 7;
const int MOD = (int)1e9 + 7;
const int INF = (int)1e9 + 7;
int n, m, k, tmp, t, tmp2, tmp3, tmp4, u, v, w, flag, q, ans, flag2;
int arr[MAXN][MAXN], dis[MAXN][MAXN], dp[4][MAXN][MAXN]; pii start, final;
vector<pair<pii, int>> adj[MAXN][MAXN]; char ch;
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> pq;
inline void add_edge(pii x, pii y, int w=1) { adj[x.F][x.S].pb({y, w}); }
int32_t main() {
#ifdef LOCAL
freopen("inp.in", "r", stdin);
freopen("res.out", "w", stdout);
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
cin >> n >> m;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
cin >> ch;
if (ch == 'C') final = {i, j}, arr[i][j] = 1;
else if (ch == 'S') start = {i, j}, arr[i][j] = 1;
else if (ch == '.') arr[i][j] = 1;
add_edge({i, j}, {i+1, j}); add_edge({i, j}, {i-1, j});
add_edge({i, j}, {i, j+1}); add_edge({i, j}, {i, j-1});
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (!arr[i][j]) dp[0][i][j] = 0;
else dp[0][i][j] = dp[0][i-1][j]+1;
if (!arr[i][j]) dp[3][i][j] = 0;
else dp[3][i][j] = dp[3][i][j-1]+1;
}
}
for (int i=n; i>=1; i--) {
for (int j=m; j>=1; j--) {
if (!arr[i][j]) dp[1][i][j] = 0;
else dp[1][i][j] = dp[1][i][j+1]+1;
if (!arr[i][j]) dp[2][i][j] = 0;
else dp[2][i][j] = dp[2][i+1][j]+1;
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (!arr[i][j]) continue;
tmp = min({dp[0][i][j], dp[1][i][j], dp[2][i][j], dp[3][i][j]});
add_edge({i, j}, {i-dp[0][i][j]+1, j}, tmp);
add_edge({i, j}, {i+dp[2][i][j]-1, j}, tmp);
add_edge({i, j}, {i, j-dp[3][i][j]+1}, tmp);
add_edge({i, j}, {i, j+dp[1][i][j]-1}, tmp);
}
}
memset(dis, 1, sizeof(dis));
dis[start.F][start.S] = 0; pq.push({0, start});
while (pq.size()) {
auto cur = pq.top(); pq.pop();
if (dis[cur.S.F][cur.S.S] != cur.F) continue;
pii u = cur.S;
if (u == final) kill(cur.F);
for (auto cur:adj[u.F][u.S]) {
if (arr[cur.F.F][cur.F.S] && dis[cur.F.F][cur.F.S] > dis[u.F][u.S]+cur.S) {
dis[cur.F.F][cur.F.S] = dis[u.F][u.S]+cur.S;
pq.push({dis[cur.F.F][cur.F.S], cur.F});
}
}
}
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... |