#include <bits/stdc++.h>
using namespace std;
#define INF (int)1e9
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
const int N = 1005;
bool a[N][N];
int d[N][N];
int x, y;
vector <pair<int, int>> ok[N][N];
bool good(int x, int y){
if (!a[x - 1][y]) return true;
if (!a[x + 1][y]) return true;
if (!a[x][y - 1]) return true;
if (!a[x][y + 1]) return true;
return false;
}
void Solve()
{
cin >> n >> m;
queue <pair<int, int>> q;
for (int i = 1; i <= n; i++){
string s; cin >> s;
for (int j = 1; j <= m; j++){
ok[i][j].push_back({i - 1, j});
ok[i][j].push_back({i, j + 1});
ok[i][j].push_back({i + 1, j});
ok[i][j].push_back({i, j - 1});
d[i][j] = INF;
if (s[j - 1] == 'S'){
a[i][j] = true;
d[i][j] = 0;
q.push({i, j});
} else if (s[j - 1] == 'C'){
a[i][j] = true;
x = i;
y = j;
} else if (s[j - 1] == '.'){
a[i][j] = true;
}
}
}
for (int i = 1; i <= n; i++){
int last = 0;
for (int j = 1; j <= m; j++){
if (good(i, j))
ok[i][j].push_back({i, last});
if (!a[i][j]) last = 0;
else if (last == 0) last = j;
}
last = 0;
for (int j = m; j >= 1; j--){
if (good(i, j))
ok[i][j].push_back({i, last});
if (!a[i][j]) last = 0;
else if (last == 0) last = j;
}
}
for (int j = 1; j <= m; j++){
int last = 0;
for (int i = 1; i <= n; i++){
if (good(i, j))
ok[i][j].push_back({last, j});
if (!a[i][j]) last = 0;
else if (last == 0) last = i;
}
last = 0;
for (int i = n; i >= 1; i--){
if (good(i, j))
ok[i][j].push_back({last, j});
if (!a[i][j]) last = 0;
else if (last == 0) last = i;
}
}
while (!q.empty()){
auto pi = q.front(); q.pop();
int ux = pi.first, uy = pi.second;
// cout << ux << " " << uy << "\n";
for (auto [vx, vy] : ok[ux][uy]){
if (!a[vx][vy]) continue;
if (d[vx][vy] == INF){
d[vx][vy] = d[ux][uy] + 1;
q.push({vx, vy});
}
}
}
cout << d[x][y] << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message
portals.cpp: In function 'void Solve()':
portals.cpp:54:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
54 | if (good(i, j))
| ^~
portals.cpp:56:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
56 | if (!a[i][j]) last = 0;
| ^~
portals.cpp:62:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
62 | if (good(i, j))
| ^~
portals.cpp:64:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
64 | if (!a[i][j]) last = 0;
| ^~
portals.cpp:72:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
72 | if (good(i, j))
| ^~
portals.cpp:74:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
74 | if (!a[i][j]) last = 0;
| ^~
portals.cpp:80:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
80 | if (good(i, j))
| ^~
portals.cpp:82:4: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
82 | if (!a[i][j]) last = 0;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24020 KB |
Output is correct |
2 |
Correct |
11 ms |
24020 KB |
Output is correct |
3 |
Correct |
11 ms |
24116 KB |
Output is correct |
4 |
Correct |
12 ms |
24020 KB |
Output is correct |
5 |
Correct |
11 ms |
24020 KB |
Output is correct |
6 |
Correct |
11 ms |
24020 KB |
Output is correct |
7 |
Correct |
12 ms |
24004 KB |
Output is correct |
8 |
Correct |
12 ms |
24020 KB |
Output is correct |
9 |
Correct |
11 ms |
24020 KB |
Output is correct |
10 |
Incorrect |
12 ms |
24020 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
23992 KB |
Output is correct |
2 |
Correct |
11 ms |
23992 KB |
Output is correct |
3 |
Correct |
11 ms |
24020 KB |
Output is correct |
4 |
Correct |
11 ms |
24020 KB |
Output is correct |
5 |
Correct |
12 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
23996 KB |
Output is correct |
7 |
Correct |
12 ms |
24120 KB |
Output is correct |
8 |
Correct |
12 ms |
24020 KB |
Output is correct |
9 |
Correct |
12 ms |
24336 KB |
Output is correct |
10 |
Incorrect |
13 ms |
24440 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24020 KB |
Output is correct |
2 |
Correct |
13 ms |
24020 KB |
Output is correct |
3 |
Correct |
11 ms |
24020 KB |
Output is correct |
4 |
Correct |
12 ms |
24020 KB |
Output is correct |
5 |
Correct |
21 ms |
28116 KB |
Output is correct |
6 |
Correct |
22 ms |
28116 KB |
Output is correct |
7 |
Correct |
22 ms |
28116 KB |
Output is correct |
8 |
Correct |
19 ms |
27988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24020 KB |
Output is correct |
2 |
Correct |
12 ms |
24020 KB |
Output is correct |
3 |
Correct |
14 ms |
24008 KB |
Output is correct |
4 |
Correct |
12 ms |
24044 KB |
Output is correct |
5 |
Correct |
11 ms |
24020 KB |
Output is correct |
6 |
Correct |
11 ms |
24020 KB |
Output is correct |
7 |
Correct |
12 ms |
24052 KB |
Output is correct |
8 |
Correct |
14 ms |
24148 KB |
Output is correct |
9 |
Correct |
12 ms |
24404 KB |
Output is correct |
10 |
Incorrect |
12 ms |
24436 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
24020 KB |
Output is correct |
2 |
Correct |
12 ms |
24040 KB |
Output is correct |
3 |
Correct |
11 ms |
24092 KB |
Output is correct |
4 |
Correct |
11 ms |
24020 KB |
Output is correct |
5 |
Correct |
13 ms |
24020 KB |
Output is correct |
6 |
Correct |
12 ms |
24020 KB |
Output is correct |
7 |
Correct |
11 ms |
24060 KB |
Output is correct |
8 |
Correct |
14 ms |
24124 KB |
Output is correct |
9 |
Correct |
13 ms |
24348 KB |
Output is correct |
10 |
Incorrect |
12 ms |
24440 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |