// Solid <candgenesis>
#include <bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 1e3 + 12;
const ll inf = 1e9 + 7;
typedef pair<ll,ll> LL;
char a[N][N];
ll n,m,i,j,d[N][N];
LL dp[N][N][4],start,en,direct[4];
vector<LL> g[N][N];
bool chk(ll x,ll y){
return (x >= 1&&y >= 1&&x <= n&&y <= m);
}
ll dx[4] = {0,0,1,-1};
ll dy[4] = {1,-1,0,0};
LL DFS(ll x,ll y,ll cond){
ll p = x + dx[cond],q = y + dy[cond];
if (a[p][q] == '#') return {x,y};
if (dp[p][q][cond].fs != 0) return dp[p][q][cond];
return dp[p][q][cond] = DFS(p,q,cond);
}
struct Node{
ll val,x,y;
};
bool operator < (Node x,Node y){
return (x.val < y.val)||(x.val == y.val&&x.x < y.x)||(x.val == y.val&&x.x == y.x&&x.y < y.y);
}
void Dij(ll x,ll y){
set<Node> s; d[x][y] = 0;
s.insert({d[x][y],x,y});
while(!s.empty()){
Node t = *s.begin(); s.erase(s.begin());
ll val = t.val;
for (auto i : g[t.x][t.y]){
ll u = i.fs,v = i.sc;
if (a[u][v] == '#') continue;
if (d[u][v] > val + 1){
s.erase({d[u][v],u,v});
d[u][v] = val + 1;
s.insert({d[u][v],u,v});
}
}
ll minn = inf;
for (ll i = 0;i < 4;i++){
direct[i] = DFS(t.x,t.y,i);
minn = min(minn,abs(direct[i].fs - t.x) + abs(direct[i].sc - t.y));
}
for (ll i = 0;i < 4;i++){
ll u = direct[i].fs,v = direct[i].sc;
if (d[u][v] > val + 1 + minn){
s.erase({d[u][v],u,v});
d[u][v] = val + 1 + minn;
s.insert({d[u][v],u,v});
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
#define task "tst"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
//freopen(task".out", "w", stdout);
}
cin>>n>>m;
for (i = 0;i <= n + 1;i++){
for (j = 0;j <= m + 1;j++){
if (!i||!j||j > m||i > n) a[i][j] = '#';
else{
cin>>a[i][j]; d[i][j] = inf;
if (a[i][j] == 'S') start = {i,j};
if (a[i][j] == 'C') en = {i,j};
for (ll k = 0;k < 4;k++){
ll p = i + dx[k],q = j + dy[k];
if (chk(p,q)) g[i][j].push_back({p,q});
}
}
}
}
Dij(start.fs,start.sc);
cout<<d[en.fs][en.sc];
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
67 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
24448 KB |
Output is correct |
2 |
Correct |
17 ms |
24448 KB |
Output is correct |
3 |
Correct |
17 ms |
24448 KB |
Output is correct |
4 |
Correct |
17 ms |
24448 KB |
Output is correct |
5 |
Correct |
17 ms |
24448 KB |
Output is correct |
6 |
Correct |
17 ms |
24448 KB |
Output is correct |
7 |
Correct |
17 ms |
24448 KB |
Output is correct |
8 |
Correct |
19 ms |
24448 KB |
Output is correct |
9 |
Correct |
17 ms |
24448 KB |
Output is correct |
10 |
Correct |
17 ms |
24448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
24448 KB |
Output is correct |
2 |
Correct |
17 ms |
24448 KB |
Output is correct |
3 |
Correct |
17 ms |
24448 KB |
Output is correct |
4 |
Correct |
17 ms |
24448 KB |
Output is correct |
5 |
Correct |
17 ms |
24448 KB |
Output is correct |
6 |
Correct |
17 ms |
24448 KB |
Output is correct |
7 |
Correct |
17 ms |
24448 KB |
Output is correct |
8 |
Correct |
18 ms |
24448 KB |
Output is correct |
9 |
Correct |
18 ms |
25088 KB |
Output is correct |
10 |
Correct |
18 ms |
25088 KB |
Output is correct |
11 |
Correct |
18 ms |
25088 KB |
Output is correct |
12 |
Correct |
20 ms |
25088 KB |
Output is correct |
13 |
Correct |
18 ms |
25088 KB |
Output is correct |
14 |
Correct |
17 ms |
24448 KB |
Output is correct |
15 |
Correct |
18 ms |
25088 KB |
Output is correct |
16 |
Correct |
17 ms |
24448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
24448 KB |
Output is correct |
2 |
Correct |
17 ms |
24448 KB |
Output is correct |
3 |
Correct |
17 ms |
24576 KB |
Output is correct |
4 |
Correct |
17 ms |
24448 KB |
Output is correct |
5 |
Correct |
33 ms |
29304 KB |
Output is correct |
6 |
Correct |
36 ms |
29304 KB |
Output is correct |
7 |
Correct |
36 ms |
29304 KB |
Output is correct |
8 |
Correct |
31 ms |
29312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
24448 KB |
Output is correct |
2 |
Correct |
17 ms |
24448 KB |
Output is correct |
3 |
Correct |
17 ms |
24448 KB |
Output is correct |
4 |
Correct |
17 ms |
24448 KB |
Output is correct |
5 |
Correct |
17 ms |
24448 KB |
Output is correct |
6 |
Correct |
17 ms |
24448 KB |
Output is correct |
7 |
Correct |
19 ms |
24448 KB |
Output is correct |
8 |
Correct |
18 ms |
24448 KB |
Output is correct |
9 |
Correct |
18 ms |
25088 KB |
Output is correct |
10 |
Correct |
18 ms |
25088 KB |
Output is correct |
11 |
Correct |
18 ms |
25088 KB |
Output is correct |
12 |
Correct |
18 ms |
25088 KB |
Output is correct |
13 |
Correct |
18 ms |
25088 KB |
Output is correct |
14 |
Correct |
33 ms |
29304 KB |
Output is correct |
15 |
Correct |
34 ms |
29304 KB |
Output is correct |
16 |
Correct |
36 ms |
29312 KB |
Output is correct |
17 |
Correct |
35 ms |
29312 KB |
Output is correct |
18 |
Correct |
38 ms |
29396 KB |
Output is correct |
19 |
Correct |
41 ms |
29432 KB |
Output is correct |
20 |
Correct |
41 ms |
29432 KB |
Output is correct |
21 |
Correct |
34 ms |
29304 KB |
Output is correct |
22 |
Correct |
35 ms |
29304 KB |
Output is correct |
23 |
Correct |
36 ms |
29440 KB |
Output is correct |
24 |
Correct |
41 ms |
29312 KB |
Output is correct |
25 |
Correct |
20 ms |
24480 KB |
Output is correct |
26 |
Correct |
18 ms |
25088 KB |
Output is correct |
27 |
Correct |
19 ms |
24448 KB |
Output is correct |
28 |
Correct |
36 ms |
29304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
24448 KB |
Output is correct |
2 |
Correct |
17 ms |
24448 KB |
Output is correct |
3 |
Correct |
18 ms |
24448 KB |
Output is correct |
4 |
Correct |
17 ms |
24448 KB |
Output is correct |
5 |
Correct |
19 ms |
24448 KB |
Output is correct |
6 |
Correct |
18 ms |
24576 KB |
Output is correct |
7 |
Correct |
18 ms |
24448 KB |
Output is correct |
8 |
Correct |
18 ms |
24448 KB |
Output is correct |
9 |
Correct |
18 ms |
25088 KB |
Output is correct |
10 |
Correct |
18 ms |
25088 KB |
Output is correct |
11 |
Correct |
18 ms |
25088 KB |
Output is correct |
12 |
Correct |
18 ms |
25088 KB |
Output is correct |
13 |
Correct |
21 ms |
25088 KB |
Output is correct |
14 |
Correct |
34 ms |
29432 KB |
Output is correct |
15 |
Correct |
39 ms |
29440 KB |
Output is correct |
16 |
Correct |
36 ms |
29304 KB |
Output is correct |
17 |
Correct |
36 ms |
29312 KB |
Output is correct |
18 |
Correct |
39 ms |
29560 KB |
Output is correct |
19 |
Correct |
43 ms |
29312 KB |
Output is correct |
20 |
Correct |
40 ms |
29560 KB |
Output is correct |
21 |
Correct |
33 ms |
29380 KB |
Output is correct |
22 |
Correct |
35 ms |
29304 KB |
Output is correct |
23 |
Correct |
37 ms |
29432 KB |
Output is correct |
24 |
Correct |
555 ms |
108300 KB |
Output is correct |
25 |
Correct |
985 ms |
108796 KB |
Output is correct |
26 |
Correct |
763 ms |
109176 KB |
Output is correct |
27 |
Correct |
761 ms |
109560 KB |
Output is correct |
28 |
Correct |
437 ms |
109176 KB |
Output is correct |
29 |
Correct |
504 ms |
109048 KB |
Output is correct |
30 |
Correct |
585 ms |
109012 KB |
Output is correct |
31 |
Correct |
39 ms |
29312 KB |
Output is correct |
32 |
Correct |
813 ms |
109176 KB |
Output is correct |
33 |
Correct |
17 ms |
24448 KB |
Output is correct |
34 |
Correct |
21 ms |
25088 KB |
Output is correct |
35 |
Correct |
615 ms |
103160 KB |
Output is correct |
36 |
Correct |
17 ms |
24448 KB |
Output is correct |
37 |
Correct |
31 ms |
29304 KB |
Output is correct |
38 |
Correct |
400 ms |
109048 KB |
Output is correct |
39 |
Correct |
443 ms |
95352 KB |
Output is correct |