// https://oj.uz/problem/view/BOI14_portals
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(0);
#define FASTIOF ios_base::sync_with_stdio(false); fin.tie(0);
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); }
ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T> void remDup(vector<T>& v) { sor(v); v.erase(unique(all(v)), v.end()); }
const int MOD = 1e9 + 7;
const int MX = 1e3 + 10;
const ll INF = 1e18;
// DIR: 0 = north, 1 = east, 2 = south, 3 = west
const int DIR[4][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
int R, C, SX, SY, CX, CY; char G[MX][MX]; array<pi, 4> nxt[MX][MX];
int distToWall[MX][MX], minDist[MX][MX];
bool isWall(int i, int j) {
return i <= 0 || i > R || j <= 0 || j > C || G[i][j] == '#';
}
void genDistToWall() {
FOR(i, 1, R + 1) {
FOR(j, 1, C + 1) {
distToWall[i][j] = MOD;
}
}
queue<pi> Q;
F0R(i, R + 2) {
F0R(j, C + 2) {
if (isWall(i, j)) {
Q.push({i, j});
distToWall[i][j] = 0;
}
}
}
while (!Q.empty()) {
pi cur = Q.front(); Q.pop();
F0R(i, 4) {
int nX = cur.f + DIR[i][0];
int nY = cur.s + DIR[i][1];
if (distToWall[nX][nY] != MOD) {
continue;
}
Q.push({nX, nY});
distToWall[nX][nY] = distToWall[cur.f][cur.s] + 1;
}
}
}
int main() {
FASTIO;
cin >> R >> C;
FOR(i, 1, R + 1) {
FOR(j, 1, C + 1) {
cin >> G[i][j];
if (G[i][j] == 'S') {
SX = i, SY = j;
}
if (G[i][j] == 'C') {
CX = i, CY = j;
}
}
}
FOR(j, 1, C + 1) {
pi lst = {1, j};
FOR(i, 1, R + 1) {
if (G[i][j] != '#') {
nxt[i][j][0] = lst;
}
else {
lst = {i + 1, j};
}
}
lst = {R, j};
ROF(i, 1, R + 1) {
if (G[i][j] != '#') {
nxt[i][j][2] = lst;
}
else {
lst = {i - 1, j};
}
}
}
FOR(i, 1, R + 1) {
pi lst = {i, C};
ROF(j, 1, C + 1) {
if (G[i][j] != '#') {
nxt[i][j][1] = lst;
}
else {
lst = {i, j - 1};
}
}
lst = {i, 1};
FOR(j, 1, C + 1) {
if (G[i][j] != '#') {
nxt[i][j][3] = lst;
}
else {
lst = {i, j + 1};
}
}
}
// FOR(i, 1, R + 1) {
// FOR(j, 1, C + 1) {
// cout << "{" << nxt[i][j][1].f << ", " << nxt[i][j][1].s << "} ";
// }
// cout << "\n";
// }
genDistToWall();
FOR(i, 1, R + 1) {
FOR(j, 1, C + 1) {
minDist[i][j] = MOD;
}
}
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> PQ;
PQ.push({0, SX, SY});
minDist[SX][SY] = 0;
while (!PQ.empty()) {
array<int, 3> cur = PQ.top(); PQ.pop();
if (cur[1] == CX && cur[2] == CY) {
return cout << minDist[CX][CY], 0;
}
if (cur[0] > minDist[cur[1]][cur[2]]) {
continue;
}
F0R(i, 4) {
int nX = cur[1] + DIR[i][0];
int nY = cur[2] + DIR[i][1];
if (isWall(nX, nY)) {
continue;
}
if (cur[0] + 1 < minDist[nX][nY]) {
minDist[nX][nY] = cur[0] + 1;
PQ.push({minDist[nX][nY], nX, nY});
}
}
int closeDist = distToWall[cur[1]][cur[2]];
F0R(i, 4) {
int nX = nxt[cur[1]][cur[2]][i].f;
int nY = nxt[cur[1]][cur[2]][i].s;
if (isWall(nX, nY)) {
continue;
}
if (cur[0] + closeDist < minDist[nX][nY]) {
minDist[nX][nY] = cur[0] + closeDist;
PQ.push({minDist[nX][nY], nX, nY});
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
460 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
976 KB |
Output is correct |
12 |
Correct |
1 ms |
976 KB |
Output is correct |
13 |
Correct |
1 ms |
976 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
984 KB |
Output is correct |
16 |
Correct |
0 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
8 ms |
4300 KB |
Output is correct |
6 |
Correct |
8 ms |
4300 KB |
Output is correct |
7 |
Correct |
6 ms |
4396 KB |
Output is correct |
8 |
Correct |
4 ms |
4300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
460 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
460 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
2 ms |
976 KB |
Output is correct |
12 |
Correct |
1 ms |
976 KB |
Output is correct |
13 |
Correct |
1 ms |
976 KB |
Output is correct |
14 |
Correct |
7 ms |
4304 KB |
Output is correct |
15 |
Correct |
9 ms |
4432 KB |
Output is correct |
16 |
Correct |
8 ms |
4432 KB |
Output is correct |
17 |
Correct |
7 ms |
4304 KB |
Output is correct |
18 |
Correct |
8 ms |
4304 KB |
Output is correct |
19 |
Correct |
3 ms |
4176 KB |
Output is correct |
20 |
Correct |
7 ms |
4248 KB |
Output is correct |
21 |
Correct |
6 ms |
4304 KB |
Output is correct |
22 |
Correct |
7 ms |
4308 KB |
Output is correct |
23 |
Correct |
7 ms |
4304 KB |
Output is correct |
24 |
Correct |
8 ms |
4176 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
976 KB |
Output is correct |
27 |
Correct |
1 ms |
336 KB |
Output is correct |
28 |
Correct |
5 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
0 ms |
460 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
976 KB |
Output is correct |
12 |
Correct |
1 ms |
976 KB |
Output is correct |
13 |
Correct |
1 ms |
976 KB |
Output is correct |
14 |
Correct |
6 ms |
4408 KB |
Output is correct |
15 |
Correct |
7 ms |
4304 KB |
Output is correct |
16 |
Correct |
7 ms |
4444 KB |
Output is correct |
17 |
Correct |
7 ms |
4360 KB |
Output is correct |
18 |
Correct |
8 ms |
4388 KB |
Output is correct |
19 |
Correct |
5 ms |
4176 KB |
Output is correct |
20 |
Correct |
10 ms |
4176 KB |
Output is correct |
21 |
Correct |
7 ms |
4424 KB |
Output is correct |
22 |
Correct |
7 ms |
4304 KB |
Output is correct |
23 |
Correct |
9 ms |
4304 KB |
Output is correct |
24 |
Correct |
204 ms |
44480 KB |
Output is correct |
25 |
Correct |
312 ms |
42324 KB |
Output is correct |
26 |
Correct |
83 ms |
41880 KB |
Output is correct |
27 |
Correct |
159 ms |
41884 KB |
Output is correct |
28 |
Correct |
156 ms |
45492 KB |
Output is correct |
29 |
Correct |
192 ms |
46480 KB |
Output is correct |
30 |
Correct |
186 ms |
46500 KB |
Output is correct |
31 |
Correct |
8 ms |
4236 KB |
Output is correct |
32 |
Correct |
259 ms |
41888 KB |
Output is correct |
33 |
Correct |
0 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
988 KB |
Output is correct |
35 |
Correct |
176 ms |
37940 KB |
Output is correct |
36 |
Correct |
0 ms |
336 KB |
Output is correct |
37 |
Correct |
4 ms |
4432 KB |
Output is correct |
38 |
Correct |
101 ms |
44676 KB |
Output is correct |
39 |
Correct |
118 ms |
32044 KB |
Output is correct |