#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)
using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;
//Note to self: Check for overflow
int n,m;
bool vis[1003][1003];
char tab[1003][1003];
bool portal[1003][1003];
vector<pii> placed;
bool moze(int i, int j)
{
if (i<0 || i>=n) return false;
if (j<0 || j>=m) return false;
if (tab[i][j]=='#') return false;
return true;
}
pii levo[1003][1003];
pii desno[1003][1003];
pii gore[1003][1003];
pii dole[1003][1003];
vector<pii> bfs[2'000'003];
int dist(int x, int y, pii sta)
{
return abs(x-sta.xx) + abs(y-sta.yy);
}
int main()
{
FAST;
cin>>n>>m;
ff(i,0,n) cin>>tab[i];
ff(i,0,n) ff(j,0,m)
{
levo[i][j]={i,j};
desno[i][j]={i,j};
gore[i][j]={i,j};
dole[i][j]={i,j};
}
ff(i,0,n) ff(j,0,m)
{
if (moze(i-1,j)) gore[i][j]=gore[i-1][j];
if (moze(i,j-1)) levo[i][j]=levo[i][j-1];
}
bff(i,0,n) bff(j,0,m)
{
if (moze(i+1,j)) dole[i][j]=dole[i+1][j];
if (moze(i,j+1)) desno[i][j]=desno[i][j+1];
}
ff(i,0,n) ff(j,0,m)
{
if (tab[i][j]=='S') bfs[0].pb({i,j});
}
fff(d,0,1'000'000) while (!bfs[d].empty())
{
auto [x,y]=bfs[d].back();
bfs[d].popb();
if (vis[x][y]) continue;
vis[x][y]=true;
if (tab[x][y]=='C') return cout<<d,0;
pii U=gore[x][y];
pii D=dole[x][y];
pii L=levo[x][y];
pii R=desno[x][y];
int md=1'000'001;
md=min(md,dist(x,y,U));
md=min(md,dist(x,y,L));
md=min(md,dist(x,y,R));
md=min(md,dist(x,y,D));
bfs[d+md+1].pb(U);
bfs[d+md+1].pb(D);
bfs[d+md+1].pb(L);
bfs[d+md+1].pb(R);
if (moze(x+1,y)) bfs[d+1].pb({x+1,y});
if (moze(x-1,y)) bfs[d+1].pb({x-1,y});
if (moze(x,y+1)) bfs[d+1].pb({x,y+1});
if (moze(x,y-1)) bfs[d+1].pb({x,y-1});
}
}
//Note to self: Check for overflow
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
47308 KB |
Output is correct |
2 |
Correct |
26 ms |
47412 KB |
Output is correct |
3 |
Correct |
24 ms |
47444 KB |
Output is correct |
4 |
Correct |
25 ms |
47416 KB |
Output is correct |
5 |
Correct |
25 ms |
47384 KB |
Output is correct |
6 |
Correct |
24 ms |
47388 KB |
Output is correct |
7 |
Correct |
24 ms |
47496 KB |
Output is correct |
8 |
Correct |
26 ms |
47436 KB |
Output is correct |
9 |
Correct |
25 ms |
47368 KB |
Output is correct |
10 |
Correct |
24 ms |
47308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47232 KB |
Output is correct |
2 |
Correct |
27 ms |
47412 KB |
Output is correct |
3 |
Correct |
28 ms |
47472 KB |
Output is correct |
4 |
Correct |
24 ms |
47312 KB |
Output is correct |
5 |
Correct |
24 ms |
47504 KB |
Output is correct |
6 |
Correct |
27 ms |
47444 KB |
Output is correct |
7 |
Correct |
23 ms |
47420 KB |
Output is correct |
8 |
Correct |
25 ms |
47428 KB |
Output is correct |
9 |
Correct |
28 ms |
48496 KB |
Output is correct |
10 |
Correct |
25 ms |
48468 KB |
Output is correct |
11 |
Correct |
25 ms |
48400 KB |
Output is correct |
12 |
Correct |
27 ms |
48376 KB |
Output is correct |
13 |
Correct |
27 ms |
48340 KB |
Output is correct |
14 |
Correct |
29 ms |
47296 KB |
Output is correct |
15 |
Correct |
28 ms |
48320 KB |
Output is correct |
16 |
Correct |
27 ms |
47388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
47248 KB |
Output is correct |
2 |
Correct |
26 ms |
47428 KB |
Output is correct |
3 |
Correct |
27 ms |
47432 KB |
Output is correct |
4 |
Correct |
28 ms |
47488 KB |
Output is correct |
5 |
Correct |
32 ms |
53716 KB |
Output is correct |
6 |
Correct |
35 ms |
53700 KB |
Output is correct |
7 |
Correct |
32 ms |
53292 KB |
Output is correct |
8 |
Correct |
42 ms |
54300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
47284 KB |
Output is correct |
2 |
Correct |
27 ms |
47444 KB |
Output is correct |
3 |
Correct |
26 ms |
47408 KB |
Output is correct |
4 |
Correct |
28 ms |
47308 KB |
Output is correct |
5 |
Correct |
28 ms |
47484 KB |
Output is correct |
6 |
Correct |
30 ms |
47396 KB |
Output is correct |
7 |
Correct |
28 ms |
47388 KB |
Output is correct |
8 |
Correct |
28 ms |
47380 KB |
Output is correct |
9 |
Correct |
31 ms |
48424 KB |
Output is correct |
10 |
Correct |
25 ms |
48336 KB |
Output is correct |
11 |
Correct |
26 ms |
48332 KB |
Output is correct |
12 |
Correct |
25 ms |
48372 KB |
Output is correct |
13 |
Correct |
25 ms |
48352 KB |
Output is correct |
14 |
Correct |
33 ms |
53708 KB |
Output is correct |
15 |
Correct |
30 ms |
53712 KB |
Output is correct |
16 |
Correct |
30 ms |
53232 KB |
Output is correct |
17 |
Correct |
30 ms |
53708 KB |
Output is correct |
18 |
Correct |
31 ms |
53724 KB |
Output is correct |
19 |
Correct |
32 ms |
52284 KB |
Output is correct |
20 |
Correct |
31 ms |
54124 KB |
Output is correct |
21 |
Correct |
31 ms |
53840 KB |
Output is correct |
22 |
Correct |
29 ms |
53588 KB |
Output is correct |
23 |
Correct |
33 ms |
53652 KB |
Output is correct |
24 |
Correct |
31 ms |
55372 KB |
Output is correct |
25 |
Correct |
28 ms |
47396 KB |
Output is correct |
26 |
Correct |
25 ms |
48308 KB |
Output is correct |
27 |
Correct |
25 ms |
47308 KB |
Output is correct |
28 |
Correct |
33 ms |
54176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
47284 KB |
Output is correct |
2 |
Correct |
23 ms |
47404 KB |
Output is correct |
3 |
Correct |
25 ms |
47432 KB |
Output is correct |
4 |
Correct |
24 ms |
47300 KB |
Output is correct |
5 |
Correct |
24 ms |
47420 KB |
Output is correct |
6 |
Correct |
26 ms |
47464 KB |
Output is correct |
7 |
Correct |
25 ms |
47412 KB |
Output is correct |
8 |
Correct |
25 ms |
47496 KB |
Output is correct |
9 |
Correct |
25 ms |
48424 KB |
Output is correct |
10 |
Correct |
27 ms |
48468 KB |
Output is correct |
11 |
Correct |
27 ms |
48340 KB |
Output is correct |
12 |
Correct |
28 ms |
48300 KB |
Output is correct |
13 |
Correct |
25 ms |
48296 KB |
Output is correct |
14 |
Correct |
32 ms |
53708 KB |
Output is correct |
15 |
Correct |
30 ms |
53736 KB |
Output is correct |
16 |
Correct |
30 ms |
53204 KB |
Output is correct |
17 |
Correct |
31 ms |
53828 KB |
Output is correct |
18 |
Correct |
33 ms |
53792 KB |
Output is correct |
19 |
Correct |
27 ms |
52180 KB |
Output is correct |
20 |
Correct |
31 ms |
54076 KB |
Output is correct |
21 |
Correct |
32 ms |
53816 KB |
Output is correct |
22 |
Correct |
30 ms |
53588 KB |
Output is correct |
23 |
Correct |
30 ms |
53588 KB |
Output is correct |
24 |
Correct |
126 ms |
119500 KB |
Output is correct |
25 |
Correct |
187 ms |
155432 KB |
Output is correct |
26 |
Correct |
57 ms |
88208 KB |
Output is correct |
27 |
Correct |
115 ms |
117752 KB |
Output is correct |
28 |
Correct |
155 ms |
121360 KB |
Output is correct |
29 |
Correct |
142 ms |
121032 KB |
Output is correct |
30 |
Correct |
120 ms |
105272 KB |
Output is correct |
31 |
Correct |
38 ms |
55528 KB |
Output is correct |
32 |
Correct |
181 ms |
154380 KB |
Output is correct |
33 |
Correct |
24 ms |
47304 KB |
Output is correct |
34 |
Correct |
25 ms |
48316 KB |
Output is correct |
35 |
Correct |
103 ms |
113484 KB |
Output is correct |
36 |
Correct |
25 ms |
47252 KB |
Output is correct |
37 |
Correct |
32 ms |
54320 KB |
Output is correct |
38 |
Correct |
210 ms |
133832 KB |
Output is correct |
39 |
Correct |
87 ms |
102824 KB |
Output is correct |