This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<str> vstr;
#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define rsz resize
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)
const double EPS = 1e-9;
const int MOD = 1e9+7; // 998244353;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};
#ifdef DEBUG
#define DBG if(1)
#else
#define DBG if(0)
#endif
#define dbg(x) cout << "(" << #x << " : " << x << ")" << endl;
// ostreams
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "["; REP(i, v.size()) out << v[i] << ", "; out << "]"; return out;}
// istreams
template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }
template<class T, class U>
istream& operator>>(istream& in, pair<T, U> &p){ in >> p.ff >> p.ss; return in; }
//searches
template<typename T, typename U>
T bsl(T lo, T hi, U f){ hi++; T mid; while(lo < hi){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid+1; } return lo; }
template<typename U>
double bsld(double lo, double hi, U f, double p = 1e-9){ int r = 3 + (int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid; } return (lo + hi)/2; }
template<typename T, typename U>
T bsh(T lo, T hi, U f){ lo--; T mid; while(lo < hi){ mid = (lo + hi + 1)/2; f(mid) ? lo = mid : hi = mid-1; } return lo; }
template<typename U>
double bshd(double lo, double hi, U f, double p = 1e-9){ int r = 3+(int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? lo = mid : hi = mid; } return (lo + hi)/2; }
// some more utility functions
template<typename T>
pair<T, int> get_min(vector<T> &v){ typename vector<T> :: iterator it = min_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T>
pair<T, int> get_max(vector<T> &v){ typename vector<T> :: iterator it = max_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T> bool ckmin(T& a, const T& b){return b < a ? a = b , true : false;}
template<typename T> bool ckmax(T& a, const T& b){return b > a ? a = b, true : false;}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int r, c;
cin >> r >> c;
vector<string> grid(r + 2, string(c + 2, '#'));
FOR(i, 1, r + 1, 1){
string s; cin >> s;
grid[i][0] = grid[i][c + 1] = '#';
FOR(j, 1, c + 1, 1) grid[i][j] = s[j - 1];
}
r += 2;
c += 2;
vector<vector<array<int, 4>>> nx(r, vector<array<int, 4>>(c, {-1, -1, -1, -1}));
REP(i, c){
int lst = 0;
REP(j, r){
if(grid[j][i] == '#')
lst = j;
nx[j][i][0] = lst;
}
REPD(j, r - 1){
if(grid[j][i] == '#')
lst = j;
nx[j][i][1] = lst;
}
}
REP(i, r){
int lst = 0;
REP(j, c){
if(grid[i][j] == '#')
lst = j;
nx[i][j][2] = lst;
}
REPD(j, c - 1){
if(grid[i][j] == '#')
lst = j;
nx[i][j][3] = lst;
}
}
vector<vector<bool>> special(r, vector<bool>(c, false));
REP(i, r){
REP(j, c){
if(grid[i][j] == '#') continue;
REP(sm, 4){
int ny = i + dy[sm];
int nx = j + dx[sm];
if(grid[ny][nx] == '#')
special[i][j] = true;
}
}
}
vector<vector<int>> dist_to_special(r, vector<int>(c, -1));
queue<array<int, 2>> q;
REP(i, r){
REP(j, c){
if(special[i][j]){
dist_to_special[i][j] = 0;
q.push({i, j});
}
}
}
while(!q.empty()){
auto v = q.front();
q.pop();
REP(sm, 4){
int ny = v[0] + dy[sm];
int nx = v[1] + dx[sm];
if(grid[ny][nx] != '#' && dist_to_special[ny][nx] == -1){
dist_to_special[ny][nx] = dist_to_special[v[0]][v[1]] + 1;
q.push({ny, nx});
}
}
}
array<int, 2> src;
array<int, 2> dest;
vector<vector<int>> dist(r, vector<int>(c, -1));
REP(i, r){
REP(j, c){
if(grid[i][j] == 'S')
src = {i, j};
if(grid[i][j] == 'C')
dest = {i, j};
}
}
const int mx = 1000000;
vector<vector<array<int, 2>>> pq(mx);
pq[0].pb(src);
REP(i, mx){
REP(k, pq[i].size()){
auto v = pq[i][k];
if(dist[v[0]][v[1]] != -1) continue;
dist[v[0]][v[1]] = i;
int row = v[0];
int col = v[1];
REP(j, 4){
int nr, nc;
if(j < 2){
nr = nx[row][col][j] + (j == 0 ? 1 : -1);
nc = col;
}
else{
nr = row;
nc = nx[row][col][j] + (j == 2 ? 1 : -1);
}
if(dist[nr][nc] == -1){
if(dist[row][col] + dist_to_special[row][col] + 1 >= mx) continue;
pq[dist[row][col] + dist_to_special[row][col] + 1].pb({nr, nc});
}
}
REP(sm, 4){
int nr = row + dy[sm];
int nc = col + dx[sm];
if(dist[nr][nc] == -1 && grid[nr][nc] != '#'){
if(dist[row][col] + dist_to_special[row][col] + 1 >= mx) continue;
pq[dist[row][col] + dist_to_special[row][col] + 1].pb({nr, nc});
}
}
}
}
cout << dist[dest[0]][dest[1]] << "\n";
return 0;
}
Compilation message (stderr)
portals.cpp: In function 'int main()':
portals.cpp:26:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
| ^
portals.cpp:28:18: note: in expansion of macro 'FOR'
28 | #define REP(i,b) FOR(i,0,b,1)
| ^~~
portals.cpp:174:9: note: in expansion of macro 'REP'
174 | REP(k, pq[i].size()){
| ^~~
portals.cpp:205:25: warning: 'dest.std::array<int, 2>::_M_elems[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
205 | cout << dist[dest[0]][dest[1]] << "\n";
| ^
portals.cpp:205:34: warning: 'dest.std::array<int, 2>::_M_elems[1]' may be used uninitialized in this function [-Wmaybe-uninitialized]
205 | cout << dist[dest[0]][dest[1]] << "\n";
| ^
# | 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... |