#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxr = 1005, maxrc = maxr * maxr;
int r, c, start = -1, finish = -1;
inline int id(const int &x, const int &y) { return x * r + y; }
int g[maxrc][4], wall[maxrc], nxt[maxrc][4], dist[maxrc];
vector<pair<int, int> > g2[maxrc];
string s[maxr];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
memset(g, -1, sizeof(g)), memset(wall, -1, sizeof(wall)), memset(nxt, -1, sizeof(nxt)), memset(dist, -1, sizeof(dist));
cin >> r >> c;
s[0] = s[r + 1] = string(c + 2, '#');
for (int i = 1; i <= r; i++) cin >> s[i], s[i].insert(s[i].begin(), '#'), s[i].push_back('#');
r += 2, c += 2;
queue<int> q;
for (int i = 0; i < r; i++) for (int j = 0; j < c; j++)
{
if (s[i][j] == 'S') start = id(i, j);
if (s[i][j] == 'C') finish = id(i, j);
if (s[i][j] == '#') wall[id(i, j)] = 0, q.push(id(i, j));
if (i && s[i - 1][j] != '#') g[id(i, j)][0] = id(i - 1, j);
if (i < r - 1 && s[i + 1][j] != '#') g[id(i, j)][1] = id(i + 1, j);
if (j && s[i][j - 1] != '#') g[id(i, j)][2] = id(i, j - 1);
if (j < c - 1 && s[i][j + 1] != '#') g[id(i, j)][3] = id(i, j + 1);
if (i && s[i - 1][j] == '#') nxt[id(i, j)][0] = id(i, j);
if (i < r - 1 && s[i + 1][j] == '#') nxt[id(i, j)][1] = id(i, j);
if (j && s[i][j - 1] == '#') nxt[id(i, j)][2] = id(i, j);
if (j < c - 1 && s[i][j + 1] == '#') nxt[id(i, j)][3] = id(i, j);
}
while (!q.empty())
{
int i = q.front();
q.pop();
for (int j = 0; j < 4; j++) if (g[i][j] != -1 && wall[g[i][j]] == -1)
wall[g[i][j]] = wall[i] + 1, q.push(g[i][j]);
}
for (int i = 1; i < r; i++) for (int j = 0; j < c; j++) if (nxt[id(i, j)][0] == -1) nxt[id(i, j)][0] = nxt[id(i - 1, j)][0];
for (int i = r-2; i >= 0; i--) for (int j = 0; j < c; j++) if (nxt[id(i, j)][1] == -1) nxt[id(i, j)][1] = nxt[id(i + 1, j)][1];
for (int i = 0; i < r; i++) for (int j = 1; j < c; j++) if (nxt[id(i, j)][2] == -1) nxt[id(i, j)][2] = nxt[id(i, j - 1)][2];
for (int i = 0; i < r; i++) for (int j = c-2; j >= 0; j--) if (nxt[id(i, j)][3] == -1) nxt[id(i, j)][3] = nxt[id(i, j + 1)][3];
for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) if (s[i][j] != '#')
{
for (int k = 0; k < 4; k++)
{
if (g[id(i, j)][k] != -1) g2[id(i, j)].push_back({ g[id(i, j)][k], 1 });
g2[id(i, j)].push_back({ nxt[id(i, j)][k], wall[id(i, j)] });
}
}
priority_queue<pair<int, int>> pq;
pq.push({ 0, start });
while (!pq.empty())
{
int u = pq.top().second, d = -pq.top().first;
pq.pop();
if (dist[u] != -1) continue;
dist[u] = d;
for (pair<int, int> v : g2[u]) if (dist[v.first] == -1)
pq.push({ -d - v.second, v.first });
}
cout << dist[finish] << "\n";
return 0;
}
Compilation message
portals.cpp: In function 'int main()':
portals.cpp:18:2: error: 'memset' was not declared in this scope
18 | memset(g, -1, sizeof(g)), memset(wall, -1, sizeof(wall)), memset(nxt, -1, sizeof(nxt)), memset(dist, -1, sizeof(dist));
| ^~~~~~
portals.cpp:6:1: note: 'memset' is defined in header '<cstring>'; did you forget to '#include <cstring>'?
5 | #include <queue>
+++ |+#include <cstring>
6 | using namespace std;