#include <bits/stdc++.h>
using namespace std;
void solve()
{
/// JUST KEEP GOING
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0};
int n, m; cin>>n>>m; char grid[n][m]; int d[n][m];
set<int>v[m], h[n]; int sx, sy, ex, ey; bool vis[n][m];
for(int l=0;l<n;l++) {
for(int i=0;i<m;i++) {
cin>>grid[l][i]; d[l][i]=1e9;
char c=grid[l][i]; vis[l][i]=false;
if(c=='S') { sx=l; sy=i; d[l][i]=0; }
if(c=='C') { ex=l; ey=i; }
}
}
for(int l=0;l<n;l++) {
for(int i=0;i<m;i++) {
if(i==0&&grid[l][i]=='.') h[l].insert(i);
else if(i==m-1&&grid[l][i]=='.') h[l].insert(i);
else if(i>0&&grid[l][i]=='.'&&grid[l][i-1]=='#') h[l].insert(i);
}
}
for(int l=0;l<m;l++) {
for(int i=0;i<n;i++) {
if(i==0&&grid[i][l]=='.') v[l].insert(i);
else if(i==n-1&&grid[i][l]=='.') v[l].insert(i);
else if(i>0&&grid[i][l]=='.'&&grid[i-1][l]=='#') v[l].insert(i);
}
}
queue<pair<int, int>>q; q.push({sx, sy});
while(!q.empty()) {
int a=q.front().first, b=q.front().second; q.pop();
for(int l=0;l<4;l++) {
int x=a+nx[l], y=b+ny[l];
if(x>=0&&x<n&&y>=0&&y<m) {
if(grid[x][y]=='#') continue;
if(d[x][y]>d[a][b]+1) {
d[x][y]=d[a][b]+1; q.push({x, y});
}
}
}
auto it=h[a].lower_bound(b);
if(it!=h[a].end()) {
if(d[a][*it]>d[a][b]+1) {
d[a][*it]=d[a][b]+1; q.push({a, *it});
}
}
if(it!=h[a].begin()) {
it--;
if(d[a][*it]>d[a][b]+1) {
d[a][*it]=d[a][b]+1; q.push({a, *it});
}
}
it=v[b].lower_bound(a);
if(it!=v[b].end()) {
if(d[*it][b]>d[a][b]+1) {
d[*it][b]=d[a][b]+1; q.push({*it, b});
}
}
if(it!=v[b].begin()) {
it--;
if(d[*it][b]>d[a][b]+1) {
d[*it][b]=d[a][b]+1; q.push({*it, b});
}
}
}
cout<<d[ex][ey]<<'\n'; return;
}
int32_t main()
{
int t; t=1;
while(t--) {
solve();
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
void solve()
{
/// JUST KEEP GOING
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0};
int n, m; cin>>n>>m; char grid[n][m]; int d[n][m];
set<int>v[m], h[n]; int sx, sy, ex, ey; bool vis[n][m];
for(int l=0;l<n;l++) {
for(int i=0;i<m;i++) {
cin>>grid[l][i]; d[l][i]=1e9;
char c=grid[l][i]; vis[l][i]=false;
if(c=='S') { sx=l; sy=i; d[l][i]=0; }
if(c=='C') { ex=l; ey=i; }
}
}
for(int l=0;l<n;l++) {
for(int i=0;i<m;i++) {
if(i==0&&grid[l][i]=='.') h[l].insert(i);
if(i==m-1&&grid[l][i]=='.') h[l].insert(i);
if(i>0&&grid[l][i]=='.'&&grid[l][i-1]=='#') h[l].insert(i);
}
}
for(int l=0;l<m;l++) {
for(int i=0;i<n;i++) {
if(i==0&&grid[i][l]=='.') v[l].insert(i);
if(i==n-1&&grid[i][l]=='.') v[l].insert(i);
if(i>0&&grid[i][l]=='.'&&grid[i-1][l]=='#') v[l].insert(i);
}
}
queue<pair<int, int>>q; q.push({sx, sy});
while(!q.empty()) {
int a=q.front().first, b=q.front().second; q.pop();
for(int l=0;l<4;l++) {
int x=a+nx[l], y=b+ny[l];
if(x>=0&&x<n&&y>=0&&y<m) {
if(grid[x][y]=='#') continue;
if(d[x][y]>d[a][b]+1) {
d[x][y]=d[a][b]+1; q.push({x, y});
}
}
}
auto it=h[a].lower_bound(b);
if(it!=h[a].end()) {
if(d[a][*it]>d[a][b]+1) {
d[a][*it]=d[a][b]+1; q.push({a, *it});
}
}
if(it!=h[a].begin()) {
it--;
if(d[a][*it]>d[a][b]+1) {
d[a][*it]=d[a][b]+1; q.push({a, *it});
}
}
it=v[b].lower_bound(a);
if(it!=v[b].end()) {
if(d[*it][b]>d[a][b]+1) {
d[*it][b]=d[a][b]+1; q.push({*it, b});
}
}
if(it!=v[b].begin()) {
it--;
if(d[*it][b]>d[a][b]+1) {
d[*it][b]=d[a][b]+1; q.push({*it, b});
}
}
}
cout<<d[ex][ey]<<'\n';
}
int32_t main()
{
int t; t=1;
while(t--) {
solve();
}
return 0;
}
Compilation message
portals.cpp:81:2: error: stray '#' in program
81 | }#include <bits/stdc++.h>
| ^
portals.cpp:81:3: error: 'include' does not name a type
81 | }#include <bits/stdc++.h>
| ^~~~~~~
portals.cpp:85:6: error: redefinition of 'void solve()'
85 | void solve()
| ^~~~~
portals.cpp:5:6: note: 'void solve()' previously defined here
5 | void solve()
| ^~~~~
portals.cpp:153:9: error: redefinition of 'int32_t main()'
153 | int32_t main()
| ^~~~
portals.cpp:73:9: note: 'int32_t main()' previously defined here
73 | int32_t main()
| ^~~~