///~~~LOTA~~~///
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define append push_back
#define add insert
#define nl "\n"
#define ff first
#define ss second
#define pii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define L0TA ios_base::sync_with_stdio(false);cin.tie(NULL)
#define N 1010
char a[N][N];
pii UP[N][N];
pii DOWN[N][N];
pii LEFT[N][N];
pii RIGHT[N][N];
int dist[N][N];
bool vis[N][N];
vector<pii> mov{{-1,0},{0,-1},{0,1},{1,0}};
void solve(){
pii pi;
vector<pii> v,u;
int n,m,p,q,r,x,y;
cin>>n>>m;
for(int i=0;i<n+4;i++){
v.append({i,1});
v.append({i,m+2});
a[i][1]=a[i][m+2]='#';
vis[i][1]=vis[i][m+2]=1;
}
for(int i=0;i<m+4;i++){
v.append({1,i});
v.append({n+2,i});
a[1][i]=a[n+2][i]='#';
vis[1][i]=vis[n+2][i]=1;
}
for(int i=2;i<n+2;i++){
for(int j=2;j<m+2;j++){
cin>>a[i][j];
if(a[i][j]=='#'){
vis[i][j]=1;
v.append({i,j});
}
else dist[i][j]=1e9;
if(a[i][j]=='S'){
p=i;
q=j;
}
if(a[i][j]=='C'){
x=i;
y=j;
}
}
}
for(int i=0;i<n*m;i++){
if(v.empty()) break;
for(auto& j:v){
dist[j.ff][j.ss]=i;
for(auto k:mov){
pi=j;
pi.ff+=k.ff;
pi.ss+=k.ss;
if(dist[pi.ff][pi.ss]>i+1){
dist[pi.ff][pi.ss]=i+1;
u.append(pi);
}
}
}
swap(v,u);
u.clear();
}
for(int i=2;i<n+2;i++){
for(int j=2;j<m+2;j++){
UP[i][j]=UP[i-1][j];
if(a[i-1][j]=='#')
UP[i][j]={i,j};
LEFT[i][j]=LEFT[i][j-1];
if(a[i][j-1]=='#')
LEFT[i][j]={i,j};
}
}
for(int i=n+1;i>1;i--){
for(int j=m+1;j>1;j--){
DOWN[i][j]=DOWN[i+1][j];
if(a[i+1][j]=='#')
DOWN[i][j]={i,j};
RIGHT[i][j]=RIGHT[i][j+1];
if(a[i][j+1]=='#')
RIGHT[i][j]={i,j};
}
}
pi={x,y};
priority_queue<pair<int,pii>,vector<pair<int,pii>>,greater<pair<int,pii>>> Q;
Q.push({0,{p,q}});
while(Q.top().ss!=pi){
p=Q.top().ss.ff;
q=Q.top().ss.ss;
r=Q.top().ff;
Q.pop();
if(!vis[p][q]){
vis[p][q]=1;
for(auto& i:mov){
if(!vis[p+i.ff][q+i.ss])
Q.push({r+1,{p+i.ff,q+i.ss}});
}
if(!vis[UP[p][q].ff][UP[p][q].ss])
Q.push({r+dist[p][q],{UP[p][q].ff,UP[p][q].ss}});
if(!vis[LEFT[p][q].ff][LEFT[p][q].ss])
Q.push({r+dist[p][q],{LEFT[p][q].ff,LEFT[p][q].ss}});
if(!vis[DOWN[p][q].ff][DOWN[p][q].ss])
Q.push({r+dist[p][q],{DOWN[p][q].ff,DOWN[p][q].ss}});
if(!vis[RIGHT[p][q].ff][RIGHT[p][q].ss])
Q.push({r+dist[p][q],{RIGHT[p][q].ff,RIGHT[p][q].ss}});
}
}
cout<<Q.top().ff;
}
int main(){
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
5720 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
2 ms |
5468 KB |
Output is correct |
12 |
Correct |
2 ms |
5464 KB |
Output is correct |
13 |
Correct |
2 ms |
5464 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
2 ms |
5468 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4548 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
9 ms |
9564 KB |
Output is correct |
6 |
Correct |
11 ms |
9560 KB |
Output is correct |
7 |
Correct |
10 ms |
9432 KB |
Output is correct |
8 |
Correct |
6 ms |
9560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4696 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
2 ms |
5468 KB |
Output is correct |
10 |
Correct |
2 ms |
5464 KB |
Output is correct |
11 |
Correct |
2 ms |
5468 KB |
Output is correct |
12 |
Correct |
2 ms |
5468 KB |
Output is correct |
13 |
Correct |
2 ms |
5468 KB |
Output is correct |
14 |
Correct |
9 ms |
9564 KB |
Output is correct |
15 |
Correct |
11 ms |
9648 KB |
Output is correct |
16 |
Correct |
10 ms |
9428 KB |
Output is correct |
17 |
Correct |
12 ms |
9428 KB |
Output is correct |
18 |
Correct |
14 ms |
9432 KB |
Output is correct |
19 |
Correct |
5 ms |
9304 KB |
Output is correct |
20 |
Correct |
13 ms |
10712 KB |
Output is correct |
21 |
Correct |
9 ms |
9564 KB |
Output is correct |
22 |
Correct |
14 ms |
9660 KB |
Output is correct |
23 |
Correct |
12 ms |
9428 KB |
Output is correct |
24 |
Correct |
12 ms |
9232 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
5468 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
6 ms |
9432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4524 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
3 ms |
5468 KB |
Output is correct |
10 |
Correct |
3 ms |
5468 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
2 ms |
5468 KB |
Output is correct |
13 |
Correct |
2 ms |
5720 KB |
Output is correct |
14 |
Correct |
9 ms |
9560 KB |
Output is correct |
15 |
Correct |
11 ms |
9564 KB |
Output is correct |
16 |
Correct |
10 ms |
9500 KB |
Output is correct |
17 |
Correct |
13 ms |
9428 KB |
Output is correct |
18 |
Correct |
17 ms |
9432 KB |
Output is correct |
19 |
Correct |
6 ms |
9308 KB |
Output is correct |
20 |
Correct |
14 ms |
10688 KB |
Output is correct |
21 |
Correct |
9 ms |
9564 KB |
Output is correct |
22 |
Correct |
9 ms |
9640 KB |
Output is correct |
23 |
Correct |
10 ms |
9560 KB |
Output is correct |
24 |
Correct |
300 ms |
44972 KB |
Output is correct |
25 |
Correct |
434 ms |
38768 KB |
Output is correct |
26 |
Correct |
87 ms |
46316 KB |
Output is correct |
27 |
Correct |
246 ms |
64616 KB |
Output is correct |
28 |
Correct |
166 ms |
47748 KB |
Output is correct |
29 |
Correct |
184 ms |
46704 KB |
Output is correct |
30 |
Correct |
187 ms |
46044 KB |
Output is correct |
31 |
Correct |
13 ms |
9052 KB |
Output is correct |
32 |
Correct |
300 ms |
38396 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
2 ms |
5468 KB |
Output is correct |
35 |
Correct |
149 ms |
42292 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
6 ms |
9432 KB |
Output is correct |
38 |
Correct |
95 ms |
48444 KB |
Output is correct |
39 |
Correct |
111 ms |
46140 KB |
Output is correct |