#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<string> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int sx, sy, cx, cy;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == 'S') {
sx = i;
sy = j;
}
if (s[i][j] == 'C') {
cx = i;
cy = j;
}
}
}
vector<vector<pair<int, int>>> up(n, vector<pair<int, int>>(m));
vector<vector<pair<int, int>>> down(n, vector<pair<int, int>>(m));
vector<vector<pair<int, int>>> left(n, vector<pair<int, int>>(m));
vector<vector<pair<int, int>>> right(n, vector<pair<int, int>>(m));
//up
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] != '#') {
if (i == 0 || up[i - 1][j] == make_pair(-1, -1)) {
up[i][j] = make_pair(i, j);
} else {
up[i][j] = up[i - 1][j];
}
} else {
up[i][j] = make_pair(-1, -1);
}
}
}
//down
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < m; j++) {
if (s[i][j] != '#') {
if (i == n - 1 || down[i + 1][j] == make_pair(-1, -1)) {
down[i][j] = make_pair(i, j);
} else {
down[i][j] = down[i + 1][j];
}
} else {
down[i][j] = make_pair(-1, -1);
}
}
}
//left
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
if (s[i][j] != '#') {
if (j == 0 || left[i][j - 1] == make_pair(-1, -1)) {
left[i][j] = make_pair(i, j);
} else {
left[i][j] = left[i][j - 1];
}
} else {
left[i][j] = make_pair(-1, -1);
}
}
}
//right
for (int j = m - 1; j >= 0; j--) {
for (int i = 0; i < n; i++) {
if (s[i][j] != '#') {
if (j == m - 1 || right[i][j + 1] == make_pair(-1, -1)) {
right[i][j] = make_pair(i, j);
} else {
right[i][j] = right[i][j + 1];
}
} else {
right[i][j] = make_pair(-1, -1);
}
}
}
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << "{" << up[i][j].first << ", " << up[i][j].second << "}" << " ";
}
cout << "\n";
}*/
vector<vector<vector<pair<int, int>>>> g(n, vector<vector<pair<int, int>>>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == '#') continue;
bool ok = false;
if (up[i][j] != make_pair(-1, -1)) {
ok = true;
}
if (down[i][j] != make_pair(-1, -1)) {
ok = true;
}
if (left[i][j] != make_pair(-1, -1)) {
ok = true;
}
if (right[i][j] != make_pair(-1, -1)) {
ok = true;
}
if (ok) {
if (up[i][j] != make_pair(-1, -1)) {
g[i][j].push_back(up[i][j]);
}
if (down[i][j] != make_pair(-1, -1)) {
g[i][j].push_back(down[i][j]);
}
if (left[i][j] != make_pair(-1, -1)) {
g[i][j].push_back(left[i][j]);
}
if (right[i][j] != make_pair(-1, -1)) {
g[i][j].push_back(right[i][j]);
}
}
for (int foo = 0; foo < 4; foo++) {
int ni = i + dx[foo], nj = j + dy[foo];
if (ni >= 0 && ni < n && nj >= 0 && nj < m && s[ni][nj] != '#') {
g[i][j].push_back(make_pair(ni, nj));
}
}
}
}
deque<pair<int, int>> q;
q.emplace_back(sx, sy);
vector<vector<int>> dist(n, vector<int>(m, -1));
dist[sx][sy] = 0;
while (!q.empty()) {
int x = q[0].first;
int y = q[0].second;
q.pop_front();
for (auto c : g[x][y]) {
if (dist[c.first][c.second] == -1) {
dist[c.first][c.second] = dist[x][y] + 1;
q.emplace_back(c.first, c.second);
}
}
}
cout << dist[cx][cy];
return 0;
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:150:22: warning: 'cy' may be used uninitialized in this function [-Wmaybe-uninitialized]
150 | cout << dist[cx][cy];
| ^
portals.cpp:150:18: warning: 'cx' may be used uninitialized in this function [-Wmaybe-uninitialized]
150 | cout << dist[cx][cy];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
11 ms |
4556 KB |
Output is correct |
6 |
Correct |
10 ms |
4556 KB |
Output is correct |
7 |
Correct |
10 ms |
4684 KB |
Output is correct |
8 |
Correct |
8 ms |
4812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |