#include <bits/stdc++.h>
#define ll int
#define pb push_back
#define fi first
#define se second
#define pll pair<ll, ll>
using namespace std;
const ll N = 1e6 + 5;
const ll mod = 1e9 + 7;
ll n, sum= 0, m, tong, k, ans, T, t;
ll b[N], d[N];
string s;
vector<pll> adj[N];
ll f(ll i, ll j)
{
return (i-1) * m + j;
}
char c[N];
queue<pll> ds;
void bfs()
{
while(!ds.empty())
{
pll u = ds.front();
ds.pop();
int j = (u.se % m == 0) ? m : (u.se % m), i = (u.se - j) / m + 1;
//cout << i <<" "<<j<<'\n';
if(i > 1)
{
k = f(i-1, j);
if(b[k] > u.fi + 1)
{
b[k] = u.fi + 1;
ds.push({b[k], k});
}
}
if(j > 1)
{
k = f(i, j-1);
if(b[k] > u.fi + 1)
{
b[k] = u.fi + 1;
ds.push({b[k], k});
}
}
if(i < n)
{
k = f(i+1, j);
if(b[k] > u.fi + 1)
{
b[k] = u.fi + 1;
ds.push({b[k], k});
}
}
if(j < m)
{
k = f(i, j+1);
if(b[k] > u.fi + 1)
{
b[k] = u.fi + 1;
ds.push({b[k], k});
}
}
}
}
inline void sol()
{
cin >> n >> m;
ll sa, fn;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
k = f(i, j);
cin >> c[k];
if(c[k] == 'S')
{
sa = k;
}
if(c[k] == 'C')
{
fn = k;
}
b[k] = mod;
d[k] = mod;
if(c[k] == '#')
{
ds.push({0, k});
b[k] = 0;
}
}
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= m; j ++)
{
k = f(i, j);
if(i > 1 && c[f(i-1, j)] != '#')adj[k].pb({1, f(i-1, j)});
if(j > 1 && c[f(i, j-1)] != '#')adj[k].pb({1, f(i, j-1)});
if(i < n && c[f(i+1, j)] != '#')adj[k].pb({1, f(i+1, j)});
if(j < m && c[f(i, j+1)] != '#')adj[k].pb({1, f(i, j+1)});
}
}
for(int i = 1; i <= n; i ++)
{
k = f(i, 1);
if(c[k] != '#')
{
b[k] = 1;
ds.push({b[k], k});
}
k = f(i, m);
if(c[k] != '#')
{
b[k] = 1;
ds.push({b[k], k});
}
}
for(int i = 1; i <= m; i ++)
{
k = f(1, i);
if(c[k] != '#')
{
b[k] = 1;
ds.push({b[k], k});
}
k = f(n, i);
if(c[k] != '#')
{
b[k] = 1;
ds.push({b[k], k});
}
}
bfs();
for(int i = 1; i <= n; i ++)
{
stack<ll> l, r;
l.push(0);
r.push(m+1);
for(int j = 1; j <= m; j ++)
{
k = f(i, j);
//cout << b[k] <<" ";
t = l.top() + 1;
adj[k].pb({b[k], f(i, t)});
if(c[k] == '#')l.push(j);
}
//cout << '\n';
for(int j = m; j > 0; j --)
{
k = f(i, j);
t = r.top() - 1;
adj[k].pb({b[k], f(i, t)});
if(c[k] == '#')r.push(j);
}
}
for(int i = 1; i <= m; i ++)
{
stack<ll> l, r;
l.push(0);
r.push(n+1);
for(int j = 1; j <= n; j ++)
{
k = f(j, i);
t = l.top() + 1;
//cout << j <<" "<< i <<" "<<t<<'\n';
adj[k].pb({b[k], f(t, i)});
if(c[k] == '#')l.push(j);
}
for(int j = n; j > 0; j --)
{
k = f(j, i);
t = r.top() - 1;
adj[k].pb({b[k], f(t, i)});
if(c[k] == '#')r.push(j);
}
}
priority_queue< pll, vector<pll>, greater<pll> >pr;
pr.push({0, sa});
d[sa] = 0;
while(!pr.empty())
{
pll u = pr.top();
pr.pop();
if(d[u.se] != u.fi)continue;
int j = (u.se % m == 0) ? m : u.se % m, i = (u.se - j) / m + 1;
//cout << i <<" "<<j<<" "<<u.fi<<'\n';
for(pll v : adj[u.se])
{
if(d[v.se] > d[u.se] + v.fi)
{
d[v.se] = d[u.se] + v.fi;
pr.push({d[v.se], v.se});
}
}
}
cout << d[fn];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
sol();
}
Compilation message
portals.cpp: In function 'void sol()':
portals.cpp:71:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
71 | for(int i = 1; i <= n; i ++)
| ^~~
portals.cpp:93:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
93 | for(int i = 1; i <= n; i ++)
| ^~~
portals.cpp:187:50: warning: unused variable 'i' [-Wunused-variable]
187 | int j = (u.se % m == 0) ? m : u.se % m, i = (u.se - j) / m + 1;
| ^
portals.cpp:181:11: warning: 'sa' may be used uninitialized in this function [-Wmaybe-uninitialized]
181 | d[sa] = 0;
| ~~~~~~^~~
portals.cpp:200:17: warning: 'fn' may be used uninitialized in this function [-Wmaybe-uninitialized]
200 | cout << d[fn];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23808 KB |
Output is correct |
2 |
Correct |
16 ms |
23808 KB |
Output is correct |
3 |
Correct |
16 ms |
23808 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
18 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23808 KB |
Output is correct |
7 |
Correct |
17 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23936 KB |
Output is correct |
9 |
Correct |
17 ms |
23808 KB |
Output is correct |
10 |
Correct |
18 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23808 KB |
Output is correct |
2 |
Correct |
18 ms |
23808 KB |
Output is correct |
3 |
Correct |
19 ms |
23808 KB |
Output is correct |
4 |
Correct |
17 ms |
23808 KB |
Output is correct |
5 |
Correct |
16 ms |
23808 KB |
Output is correct |
6 |
Correct |
18 ms |
23808 KB |
Output is correct |
7 |
Correct |
16 ms |
23808 KB |
Output is correct |
8 |
Correct |
19 ms |
23808 KB |
Output is correct |
9 |
Correct |
20 ms |
24064 KB |
Output is correct |
10 |
Correct |
18 ms |
24064 KB |
Output is correct |
11 |
Correct |
18 ms |
24064 KB |
Output is correct |
12 |
Correct |
18 ms |
24064 KB |
Output is correct |
13 |
Correct |
20 ms |
24064 KB |
Output is correct |
14 |
Correct |
17 ms |
23808 KB |
Output is correct |
15 |
Correct |
17 ms |
24064 KB |
Output is correct |
16 |
Correct |
17 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
16 ms |
23808 KB |
Output is correct |
3 |
Correct |
16 ms |
23808 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
38 ms |
27640 KB |
Output is correct |
6 |
Correct |
39 ms |
27644 KB |
Output is correct |
7 |
Correct |
37 ms |
27512 KB |
Output is correct |
8 |
Correct |
35 ms |
27736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
16 ms |
23808 KB |
Output is correct |
3 |
Correct |
17 ms |
23808 KB |
Output is correct |
4 |
Correct |
21 ms |
23936 KB |
Output is correct |
5 |
Correct |
19 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23808 KB |
Output is correct |
7 |
Correct |
17 ms |
23808 KB |
Output is correct |
8 |
Correct |
16 ms |
23808 KB |
Output is correct |
9 |
Correct |
17 ms |
24064 KB |
Output is correct |
10 |
Correct |
17 ms |
24064 KB |
Output is correct |
11 |
Correct |
18 ms |
24192 KB |
Output is correct |
12 |
Correct |
17 ms |
24064 KB |
Output is correct |
13 |
Correct |
18 ms |
24064 KB |
Output is correct |
14 |
Correct |
51 ms |
27640 KB |
Output is correct |
15 |
Correct |
46 ms |
27768 KB |
Output is correct |
16 |
Correct |
40 ms |
27520 KB |
Output is correct |
17 |
Correct |
38 ms |
27512 KB |
Output is correct |
18 |
Correct |
41 ms |
27700 KB |
Output is correct |
19 |
Correct |
44 ms |
27384 KB |
Output is correct |
20 |
Correct |
47 ms |
27392 KB |
Output is correct |
21 |
Correct |
40 ms |
27640 KB |
Output is correct |
22 |
Correct |
39 ms |
27648 KB |
Output is correct |
23 |
Correct |
38 ms |
27648 KB |
Output is correct |
24 |
Correct |
36 ms |
27392 KB |
Output is correct |
25 |
Correct |
18 ms |
23808 KB |
Output is correct |
26 |
Correct |
17 ms |
24064 KB |
Output is correct |
27 |
Correct |
17 ms |
23808 KB |
Output is correct |
28 |
Correct |
35 ms |
27640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
23808 KB |
Output is correct |
2 |
Correct |
16 ms |
23808 KB |
Output is correct |
3 |
Correct |
17 ms |
23808 KB |
Output is correct |
4 |
Correct |
17 ms |
23808 KB |
Output is correct |
5 |
Correct |
16 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23808 KB |
Output is correct |
7 |
Correct |
17 ms |
23808 KB |
Output is correct |
8 |
Correct |
16 ms |
23808 KB |
Output is correct |
9 |
Correct |
20 ms |
24064 KB |
Output is correct |
10 |
Correct |
18 ms |
24064 KB |
Output is correct |
11 |
Correct |
18 ms |
24064 KB |
Output is correct |
12 |
Correct |
18 ms |
24040 KB |
Output is correct |
13 |
Correct |
20 ms |
24064 KB |
Output is correct |
14 |
Correct |
49 ms |
27616 KB |
Output is correct |
15 |
Correct |
40 ms |
27552 KB |
Output is correct |
16 |
Correct |
48 ms |
27556 KB |
Output is correct |
17 |
Correct |
44 ms |
27512 KB |
Output is correct |
18 |
Correct |
46 ms |
27640 KB |
Output is correct |
19 |
Correct |
44 ms |
27392 KB |
Output is correct |
20 |
Correct |
40 ms |
27384 KB |
Output is correct |
21 |
Correct |
44 ms |
27640 KB |
Output is correct |
22 |
Correct |
45 ms |
27616 KB |
Output is correct |
23 |
Correct |
39 ms |
27640 KB |
Output is correct |
24 |
Correct |
780 ms |
116364 KB |
Output is correct |
25 |
Correct |
794 ms |
111224 KB |
Output is correct |
26 |
Correct |
702 ms |
111096 KB |
Output is correct |
27 |
Correct |
695 ms |
111320 KB |
Output is correct |
28 |
Correct |
801 ms |
118340 KB |
Output is correct |
29 |
Correct |
849 ms |
118624 KB |
Output is correct |
30 |
Correct |
869 ms |
118496 KB |
Output is correct |
31 |
Correct |
36 ms |
27384 KB |
Output is correct |
32 |
Correct |
712 ms |
112056 KB |
Output is correct |
33 |
Correct |
20 ms |
23808 KB |
Output is correct |
34 |
Correct |
17 ms |
24064 KB |
Output is correct |
35 |
Correct |
751 ms |
117368 KB |
Output is correct |
36 |
Correct |
19 ms |
23808 KB |
Output is correct |
37 |
Correct |
40 ms |
27776 KB |
Output is correct |
38 |
Correct |
783 ms |
119864 KB |
Output is correct |
39 |
Correct |
782 ms |
116600 KB |
Output is correct |