#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
const int N = 1010, INF = 1e9 + 69;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};
int n, m;
bool grid[N][N];
int dis1[N][N], dis2[N][N];
int thinkbook[4][N][N];
vector<pair<int,int>> w[N * N];
void solve(){
cin >> n >> m;
for(int i = 0; i <= n+1; ++i) grid[i][0] = grid[i][m+1] = 1;
for(int i = 0; i <= m+1; ++i) grid[0][i] = grid[n+1][i] = 1;
pair<int, int> s = {-1, -1}, e = {-1, -1};
for(int i = 1; i <= n; ++i) {
string line; cin >> line;
for(int j = 1; j <= m; ++j){
if (line[j-1] == '#') grid[i][j] = 1;
else if (line[j-1] == 'S') s = {i, j};
else if (line[j-1] == 'C') e = {i, j};
}
}
for(int i = 1; i<= n; ++i) for(int j = 1; j <= m; ++j) dis1[i][j] = INF;
for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) {
if (!grid[i][j-1])
thinkbook[0][i][j] = thinkbook[0][i][j-1] + 1;
if (!grid[i-1][j])
thinkbook[2][i][j] = thinkbook[2][i-1][j] + 1;
}
for(int i = n; i >= 1; --i) for(int j = m; j >= 1; --j){
if (!grid[i][j+1])
thinkbook[1][i][j] = thinkbook[1][i][j+1] + 1;
if (!grid[i+1][j])
thinkbook[3][i][j] = thinkbook[3][i+1][j] + 1;
}
deque<pair<int, int>> q;
for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) if (!grid[i][j]){
bool check = true;
for(int d = 0; d < 4; ++d) if (grid[i + dx[d]][j + dy[d]]) check = true;
if (check){
q.push_back({i, j});
dis1[i][j] = 0;
}
}
while(q.size()){
pair<int, int> u = q.front(); q.pop_front();
for(int d = 0; d < 4; ++d) {
pair<int, int> v = {u.first + dx[d], u.second + dy[d]};
if (minimize(dis1[v.first][v.second], dis1[u.first][u.second] + 1)){
q.push_back(v);
}
}
}
for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) if (!grid[i][j]){
dis2[i][j] = INF;
}
dis2[s.first][s.second] = 0;
w[0].push_back(s);
for(int i = 0; i < N * N; ++i) {
while(w[i].size()){
pair<int, int> u = w[i].back(); w[i].pop_back();
for(int d = 0; d < 4; ++d) {
pair<int, int> v = {u.first + dx[d], u.second + dy[d]};
if (minimize(dis2[v.first][v.second], dis2[u.first][u.second] + 1)){
w[i+1].push_back(v);
}
v = {u.first + dx[d] * thinkbook[d][u.first][u.second], u.second + dy[d] * thinkbook[d][u.first][u.second]};
if (minimize(dis2[v.first][v.second], dis2[u.first][u.second] + dis1[u.first][u.second] + 1)){
w[i+dis1[u.first][u.second]+1].push_back(v);
}
}
}
}
cout << dis2[e.first][e.second] << "\n";
}
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << " ms\n";
return 0;
}
# | 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... |