#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define REP(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)
#define mp make_pair
#define all(v) v.begin(), v.end()
#define uni(v) v.erase(unique(all(v)), v.end())
#define Bit(x, i) ((x >> (i)) & 1)
#define Mask(i) (1 << (i))
#define Cnt(x) __builtin_popcount(x)
#define Cntll(x) __builtin_popcountll(x)
#define Ctz(x) __builtin_ctz(x)
#define Ctzll(x) __builtin_ctzll(x)
#define Clz(x) __builtin_clz(x)
#define Clzll(x) __builtin_clzll(x)
#define left __left
#define down __down
#define right __right
#define up __up
inline bool maximize(int &u, int v){ return v > u ? u = v, true : false; }
inline bool minimize(int &u, int v){ return v < u ? u = v, true : false; }
inline bool maximizell(long long &u, long long v){ return v > u ? u = v, true : false; }
inline bool minimizell(long long &u, long long v){ return v < u ? u = v, true : false; }
const int mod = 1e9 + 7;
inline int fastPow(int a, int n){
if(n == 0) return 1;
int t = fastPow(a, n >> 1);
t = 1ll * t * t % mod;
if(n & 1) t = 1ll * t * a % mod;
return t;
}
inline void add(int &u, int v){ u += v; if(u >= mod) u -= mod; }
inline void sub(int &u, int v){ u -= v; if(u < 0) u += mod; }
const int maxN = 1e3 + 5;
const int inf = 1e9;
const long long infll = 1e18;
int up[maxN][maxN], down[maxN][maxN], left[maxN][maxN], right[maxN][maxN];
char c[maxN][maxN];
int dist[maxN][maxN];
int numRow, numCol;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
queue<pair<int, int>> q;
void punch(int u, int v, int luv, int ruv, int tuv, int buv, int cost){
if(luv == v){
if(dist[u][ruv] == inf){
dist[u][ruv] = cost;
q.emplace(u, ruv);
}
if(dist[tuv][v] == inf){
dist[tuv][v] = cost;
q.emplace(tuv, v);
}
if(dist[buv][v] == inf){
dist[buv][v] = cost;
q.emplace(buv, v);
}
}
if(ruv == v){
if(dist[u][luv] == inf){
dist[u][luv] = cost;
q.emplace(u, luv);
}
if(dist[tuv][v] == inf){
dist[tuv][v] = cost;
q.emplace(tuv, v);
}
if(dist[buv][v] == inf){
dist[buv][v] = cost;
q.emplace(buv, v);
}
}
if(tuv == u){
if(dist[u][ruv] == inf){
dist[u][ruv] = cost;
q.emplace(u, ruv);
}
if(dist[u][luv] == inf){
dist[u][luv] = cost;
q.emplace(u, luv);
}
if(dist[buv][v] == inf){
dist[buv][v] = cost;
q.emplace(buv, v);
}
}
if(buv == u){
if(dist[u][ruv] == inf){
dist[u][ruv] = cost;
q.emplace(u, ruv);
}
if(dist[u][luv] == inf){
dist[u][luv] = cost;
q.emplace(u, luv);
}
if(dist[tuv][v] == inf){
dist[tuv][v] = cost;
q.emplace(tuv, v);
}
}
}
void process(){
cin >> numRow >> numCol;
int si, sj, ei, ej;
FOR(i, 1, numRow){
FOR(j, 1, numCol){
cin >> c[i][j];
if(c[i][j] == 'S')si = i, sj = j;
if(c[i][j] == 'C')ei = i, ej = j;
dist[i][j] = inf;
}
}
FOR(i, 1, numRow){
int cur = 0;
FOR(j, 1, numCol){
if(c[i][j] == '#')cur = j;
left[i][j] = cur + 1;
}
cur = numCol + 1;
REP(j, numCol, 1){
if(c[i][j] == '#')cur = j;
right[i][j] = cur - 1;
}
}
FOR(j, 1, numCol){
int cur = 0;
FOR(i, 1, numRow){
if(c[i][j] == '#')cur = i;
up[i][j] = cur + 1;
}
cur = numRow + 1;
REP(i, numRow, 1){
if(c[i][j] == '#')cur = i;
down[i][j] = cur - 1;
}
}
dist[si][sj] = 0;
q.emplace(si, sj);
while(!q.empty()){
auto[u, v] = q.front();
q.pop();
FOR(k, 0, 3){
int x = u + dx[k];
int y = v + dy[k];
if(x >= 1 && x <= numRow && v >= 1 && v <= numCol && c[x][y] != '#' && dist[x][y] == inf){
dist[x][y] = dist[u][v] + 1;
q.emplace(x, y);
}
}
punch(u, v, left[u][v], right[u][v], up[u][v], down[u][v], dist[u][v] + 1);
}
cout << dist[ei][ej];
}
#define LOVE "code"
int main(){
if(fopen(LOVE".inp", "r")){
freopen(LOVE".inp", "r", stdin);
// freopen(LOVE".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--)
process();
// cerr << '\n' << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << " s\n" ;
return 0;
}
Compilation message (stderr)
portals.cpp: In function 'int main()':
portals.cpp:159:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
159 | freopen(LOVE".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |