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;
#define ll long long
#define pb push_back
const int mxN = 2e6 + 5, mxN1 = 2005;
ll n,m,dis[mxN],dis1[mxN],st,fi,idx[mxN1][mxN1];
pair<ll,ll> wall[mxN][5];
char a[mxN1][mxN1];
bool vis[mxN];
vector<pair<ll,ll>> v[mxN];
queue<ll> qq;
priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> q;
void bfs(){
while(!qq.empty()){
ll at = qq.front(); qq.pop();
for(auto it1 : v[at]){
ll it = it1.first;
if(vis[it]) continue;
dis1[it] = dis1[at] + 1;
vis[it] = true;
qq.push(it);
}
}
}
void f(){
for(int i = 0; i <= n + 1; i++){
for(int j = 0; j <= m + 1; j++){
if(a[i][j] == '#'){
wall[idx[i][j]][0] = make_pair(i, j + 1);
wall[idx[i][j]][1] = make_pair(i + 1, j);
}
else{
wall[idx[i][j]][0] = wall[idx[i][j - 1]][0];
wall[idx[i][j]][1] = wall[idx[i - 1][j]][1];
}
}
}
for(int i = n + 1; i >= 0; i--){
for(int j = m + 1; j >= 0; j--){
if(a[i][j] == '#'){
wall[idx[i][j]][2] = make_pair(i, j - 1);
wall[idx[i][j]][3] = make_pair(i - 1, j);
}
else{
wall[idx[i][j]][2] = wall[idx[i][j + 1]][2];
wall[idx[i][j]][3] = wall[idx[i + 1][j]][3];
}
if(a[i][j] == '#') continue;
for(int k = 0; k < 4; k++){
pair<ll,ll> x = wall[idx[i][j]][k];
if(abs(x.first - i) + abs(x.second - j) > dis1[idx[i][j]]){
v[idx[i][j]].pb({x.first * (m + 2) + x.second, dis1[idx[i][j]]});
}
}
}
}
}
void dijkstra(){
for(int i = 1; i < mxN; i++){
dis[i] = 1e9;
vis[i] = false;
}
q.push({0, st});
dis[st] = 0;
while(!q.empty()){
ll at = q.top().second; q.pop();
if(vis[at]) continue;
vis[at] = true;
// cout << at / (m + 2) << ' ' << at % (m + 2) << ' ' << dis[at] << ' ' << '\n';
for(auto it : v[at]){
if(vis[it.first]) continue;
if(dis[it.first] > dis[at] + it.second){
dis[it.first] = dis[at] + it.second;
q.push({dis[it.first], it.first});
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 0; i <= n + 1; i++){
for(int j = 0; j <= m + 1; j++){
if(i == 0 or i == n + 1 or j == 0 or j == m + 1) a[i][j] = '#';
else cin >> a[i][j];
idx[i][j] = i * (m + 2) + j;
if(a[i][j] == '#') qq.push(idx[i][j]);
if(a[i][j] == 'S'){
st = idx[i][j];
a[i][j] = '.';
}
if(a[i][j] == 'C'){
fi = idx[i][j];
a[i][j] = '.';
}
}
}
for(int i = 0; i <= n + 1; i++){
for(int j = 0; j <= m + 1; j++){
if(i > 0 and a[i - 1][j] != '#') v[idx[i][j]].pb({idx[i - 1][j], 1});
if(i < n + 1 and a[i + 1][j] != '#') v[idx[i][j]].pb({idx[i + 1][j], 1});
if(j > 0 and a[i][j - 1] != '#') v[idx[i][j]].pb({idx[i][j - 1], 1});
if(j < m + 1 and a[i][j + 1] != '#') v[idx[i][j]].pb({idx[i][j + 1], 1});
}
}
bfs();
f();
dijkstra();
cout << dis[fi];
}
# | 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... |