#include<bits/stdc++.h>
#define ll int
#define pb push_back
#define mp make_pair
#define ld long double
#define F first
#define S second
#define pii pair<ll,ll>
using namespace :: std;
const ll mod=1e9+7;
const ll maxn=1010;
const ll inf=1e9+900;
const ll maxm=maxn*maxn;
const ll di[4]={+1,-1,0,0};
const ll dj[4]={0,0,+1,-1};
vector<pii> ger[maxm];
ll dp[maxm];
vector<ll> vec[maxm];
ll fas(ll a,ll b){
vec[0].pb(a);
fill(dp,dp+maxm,inf);
for(ll w=0;w<maxm;w++){
for(auto v:vec[w]){
if(dp[v]==inf){
dp[v]=w;
if(v==b)return w;
for(auto t:ger[v]){
if(dp[t.F]==inf){
if(w+t.S<maxm){
vec[w+t.S].pb(t.F);
}
}
}
}
}
}
if(dp[b]==inf)dp[b]=-1;
return dp[b];
}
string s[maxn];
ll minn[maxm];
ll ta[maxm][4];
ll n,m;
ll getid(ll a,ll b){
return a*maxn+b;
}
bool val(ll a,ll b){
return (!(a<0 || b<0 || a>=n || b>=m || s[a][b]=='#'));
}
void addYal(ll a,ll b,ll c){
if(a==b)return;
ger[a].pb(mp(b,c));
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(ll i=0;i<n;i++){
cin>>s[i];
}
fill(minn,minn+maxm,inf);
queue<ll> qu;
for(ll i=0;i<n;i++){
for(ll j=0;j<m;j++){
ll id=getid(i,j);
if(s[i][j]!='#'){
for(ll y=0;y<4;y++){
if(val(i+di[y],j+dj[y])){
addYal(getid(i,j),getid(i+di[y],j+dj[y]),1);
}else if(minn[id]==inf){
minn[id]=1;
qu.push(id);
}
}
}
}
}
while(qu.size()){
ll v=qu.front();
qu.pop();
for(auto e:ger[v]){
ll u=e.F;
if(minn[u]==inf){
minn[u]=minn[v]+1;
qu.push(u);
}
}
}
for(ll i=0;i<n;i++){
for(ll j=0;j<m;j++){
ll id=getid(i,j);
if(val(i+di[1],j+dj[1])){
ta[id][1]=ta[getid(i+di[1],j+dj[1])][1];
}else{
ta[id][1]=id;
}
if(val(i+di[3],j+dj[3])){
ta[id][3]=ta[getid(i+di[3],j+dj[3])][3];
}else{
ta[id][3]=id;
}
}
}
for(ll i=n-1;i>=0;i--){
for(ll j=m-1;j>=0;j--){
ll id=getid(i,j);
if(val(i+di[2],j+dj[2])){
ta[id][2]=ta[getid(i+di[2],j+dj[2])][2];
}else{
ta[id][2]=id;
}
if(val(i+di[0],j+dj[0])){
ta[id][0]=ta[getid(i+di[0],j+dj[0])][0];
}else{
ta[id][0]=id;
}
}
}
ll start;
ll end;
for(ll i=0;i<n;i++){
for(ll j=0;j<m;j++){
if(s[i][j]!='#'){
ll id=getid(i,j);
if(s[i][j]=='S')start=id;
if(s[i][j]=='C')end=id;
for(ll y=0;y<4;y++){
addYal(id,ta[id][y],minn[id]);
}
}
}
}
cout<<fas(start,end);
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:145:24: warning: 'end' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout<<fas(start,end);
^
portals.cpp:145:24: warning: 'start' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
56312 KB |
Output is correct |
2 |
Correct |
52 ms |
56312 KB |
Output is correct |
3 |
Correct |
61 ms |
56316 KB |
Output is correct |
4 |
Correct |
52 ms |
56312 KB |
Output is correct |
5 |
Correct |
53 ms |
56312 KB |
Output is correct |
6 |
Correct |
51 ms |
56312 KB |
Output is correct |
7 |
Correct |
51 ms |
56312 KB |
Output is correct |
8 |
Correct |
51 ms |
56312 KB |
Output is correct |
9 |
Correct |
53 ms |
56296 KB |
Output is correct |
10 |
Correct |
51 ms |
56312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
56408 KB |
Output is correct |
2 |
Correct |
53 ms |
56312 KB |
Output is correct |
3 |
Correct |
50 ms |
56312 KB |
Output is correct |
4 |
Correct |
49 ms |
56328 KB |
Output is correct |
5 |
Correct |
50 ms |
56312 KB |
Output is correct |
6 |
Correct |
50 ms |
56440 KB |
Output is correct |
7 |
Correct |
52 ms |
56312 KB |
Output is correct |
8 |
Correct |
52 ms |
56440 KB |
Output is correct |
9 |
Correct |
54 ms |
56696 KB |
Output is correct |
10 |
Correct |
51 ms |
56824 KB |
Output is correct |
11 |
Correct |
50 ms |
56696 KB |
Output is correct |
12 |
Correct |
50 ms |
56568 KB |
Output is correct |
13 |
Correct |
51 ms |
56568 KB |
Output is correct |
14 |
Correct |
51 ms |
56364 KB |
Output is correct |
15 |
Correct |
60 ms |
56696 KB |
Output is correct |
16 |
Correct |
52 ms |
56312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
56312 KB |
Output is correct |
2 |
Correct |
51 ms |
56312 KB |
Output is correct |
3 |
Correct |
52 ms |
56312 KB |
Output is correct |
4 |
Correct |
51 ms |
56312 KB |
Output is correct |
5 |
Correct |
65 ms |
59256 KB |
Output is correct |
6 |
Correct |
64 ms |
59384 KB |
Output is correct |
7 |
Correct |
63 ms |
59896 KB |
Output is correct |
8 |
Correct |
65 ms |
59896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
56228 KB |
Output is correct |
2 |
Correct |
51 ms |
56468 KB |
Output is correct |
3 |
Correct |
53 ms |
56312 KB |
Output is correct |
4 |
Correct |
52 ms |
56312 KB |
Output is correct |
5 |
Correct |
50 ms |
56312 KB |
Output is correct |
6 |
Correct |
52 ms |
56312 KB |
Output is correct |
7 |
Correct |
49 ms |
56312 KB |
Output is correct |
8 |
Correct |
50 ms |
56312 KB |
Output is correct |
9 |
Correct |
51 ms |
56696 KB |
Output is correct |
10 |
Correct |
52 ms |
56824 KB |
Output is correct |
11 |
Correct |
50 ms |
56568 KB |
Output is correct |
12 |
Correct |
52 ms |
56568 KB |
Output is correct |
13 |
Correct |
53 ms |
56696 KB |
Output is correct |
14 |
Correct |
66 ms |
59260 KB |
Output is correct |
15 |
Correct |
65 ms |
59512 KB |
Output is correct |
16 |
Correct |
64 ms |
59768 KB |
Output is correct |
17 |
Correct |
66 ms |
59896 KB |
Output is correct |
18 |
Correct |
68 ms |
60508 KB |
Output is correct |
19 |
Correct |
67 ms |
60920 KB |
Output is correct |
20 |
Correct |
69 ms |
61560 KB |
Output is correct |
21 |
Correct |
65 ms |
59260 KB |
Output is correct |
22 |
Correct |
67 ms |
59256 KB |
Output is correct |
23 |
Correct |
66 ms |
59512 KB |
Output is correct |
24 |
Correct |
72 ms |
61284 KB |
Output is correct |
25 |
Correct |
52 ms |
56312 KB |
Output is correct |
26 |
Correct |
52 ms |
56696 KB |
Output is correct |
27 |
Correct |
60 ms |
56360 KB |
Output is correct |
28 |
Correct |
60 ms |
59896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
56240 KB |
Output is correct |
2 |
Correct |
50 ms |
56312 KB |
Output is correct |
3 |
Correct |
52 ms |
56312 KB |
Output is correct |
4 |
Correct |
49 ms |
56312 KB |
Output is correct |
5 |
Correct |
50 ms |
56440 KB |
Output is correct |
6 |
Correct |
53 ms |
56312 KB |
Output is correct |
7 |
Correct |
53 ms |
56312 KB |
Output is correct |
8 |
Correct |
52 ms |
56312 KB |
Output is correct |
9 |
Correct |
52 ms |
56696 KB |
Output is correct |
10 |
Correct |
54 ms |
56824 KB |
Output is correct |
11 |
Correct |
53 ms |
56696 KB |
Output is correct |
12 |
Correct |
52 ms |
56696 KB |
Output is correct |
13 |
Correct |
61 ms |
56568 KB |
Output is correct |
14 |
Correct |
66 ms |
59256 KB |
Output is correct |
15 |
Correct |
66 ms |
59512 KB |
Output is correct |
16 |
Correct |
61 ms |
59720 KB |
Output is correct |
17 |
Correct |
65 ms |
59900 KB |
Output is correct |
18 |
Correct |
66 ms |
60408 KB |
Output is correct |
19 |
Correct |
67 ms |
60920 KB |
Output is correct |
20 |
Correct |
72 ms |
61508 KB |
Output is correct |
21 |
Correct |
66 ms |
59256 KB |
Output is correct |
22 |
Correct |
63 ms |
59256 KB |
Output is correct |
23 |
Correct |
62 ms |
59500 KB |
Output is correct |
24 |
Correct |
435 ms |
133436 KB |
Output is correct |
25 |
Correct |
774 ms |
159536 KB |
Output is correct |
26 |
Correct |
506 ms |
154388 KB |
Output is correct |
27 |
Correct |
580 ms |
166092 KB |
Output is correct |
28 |
Correct |
376 ms |
110736 KB |
Output is correct |
29 |
Correct |
385 ms |
111712 KB |
Output is correct |
30 |
Correct |
358 ms |
111892 KB |
Output is correct |
31 |
Correct |
67 ms |
61304 KB |
Output is correct |
32 |
Correct |
657 ms |
161400 KB |
Output is correct |
33 |
Correct |
51 ms |
56312 KB |
Output is correct |
34 |
Correct |
50 ms |
56672 KB |
Output is correct |
35 |
Correct |
421 ms |
127864 KB |
Output is correct |
36 |
Correct |
49 ms |
56312 KB |
Output is correct |
37 |
Correct |
66 ms |
59896 KB |
Output is correct |
38 |
Correct |
383 ms |
125536 KB |
Output is correct |
39 |
Correct |
236 ms |
98496 KB |
Output is correct |