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;
const int N = 1002;
int dozida[N][N];
int bio[N][N];
int ubacen[N][N];
int xmov[4] = {0, 1, 0, -1};
int ymov[4] = {-1, 0, 1, 0};
void reset(){
for (int i=0; i<N; ++i){
for (int j=0; j<N; ++j){
dozida[i][j] = -1;
bio[i][j] = -1;
ubacen[i][j] = -1;
}
}
}
void bfs_odzida(const vector<string>& v, vector<pair<int, int>>& fronta){
vector<pair<int, int>> novi;
for (int step = 0; !fronta.empty(); ++step){
novi.clear();
for (int i=0; i<fronta.size(); ++i){
pair<int, int> curr = fronta[i];
if (dozida[curr.first][curr.second] != -1) continue;
dozida[curr.first][curr.second] = step;
for (int j=0; j<4; ++j){
int y = curr.first + ymov[j];
int x = curr.second + xmov[j];
if (v[y][x] == '#') continue;
if (dozida[y][x] != -1) continue;
novi.push_back(make_pair(y, x));
}
}
fronta.swap(novi);
}
}
using mpt = array<pair<int, int>, 4>;
mpt moguci_portal(const vector<string>& v, const pair<int, int>& point){
mpt ret;
for (int i=0; i<4; ++i){
for (int j=1; ; ++j){
int y = point.first + ymov[i] * j;
int x = point.second + xmov[i] * j;
if (v[y][x] == '#'){
ret[i] = make_pair(y - ymov[i], x - xmov[i]);
break;
}
}
}
return ret;
}
void dijkstra(const vector<string>& v, const pair<int, int>& start, const pair<int, int>& cilj){
using el_t = pair<int, pair<int, int>>;
auto cmp = [](const auto& l, const auto& r){return r < l;};
vector<el_t> que;
que.push_back(make_pair(0, start));
ubacen[start.first][start.second] = 0;
while (!que.empty()){
el_t poz = *que.begin();
pop_heap(que.begin(), que.end(), cmp);
que.pop_back();
pair<int, int> curr = poz.second;
if (bio[curr.first][curr.second] != -1) continue;
bio[curr.first][curr.second] = poz.first;
if (curr == cilj) return;
for (int i=0; i<4; ++i){
int y = curr.first + ymov[i];
int x = curr.second + xmov[i];
if (v[y][x] == '#') continue;
if (ubacen[y][x] != -1 && ubacen[y][x] <= poz.first + 1) continue;
que.push_back(make_pair(poz.first + 1, make_pair(y, x)));
push_heap(que.begin(), que.end(), cmp);
ubacen[y][x] = poz.first + 1;
}
auto krajevi = moguci_portal(v, curr);
for (int i=0; i<4; ++i){
int y = krajevi[i].first;
int x = krajevi[i].second;
if (ubacen[y][x] != -1 && ubacen[y][x] <= poz.first + dozida[curr.first][curr.second] + 1) continue;
que.push_back(make_pair(poz.first + dozida[curr.first][curr.second] + 1, krajevi[i]));
push_heap(que.begin(), que.end(), cmp);
ubacen[y][x] = poz.first + dozida[curr.first][curr.second] + 1;
}
}
}
int main(){
reset();
int n, m;
cin >> n >> m;
string nule = "";
for (int i=0; i<m+2; ++i){
nule += '#';
}
vector<string> v;
v.push_back(nule);
pair<int, int> start;
pair<int, int> cilj;
for (int i=0; i<n; ++i){
string s;
cin >> s;
for (int j=0; j<s.size(); ++j){
if (s[j] == 'S'){
start.first = i+1;
start.second = j+1;
}
if (s[j] == 'C'){
cilj.first = i+1;
cilj.second = j+1;
}
}
s = "#" + s + "#";
v.push_back(s);
}
v.push_back(nule);
vector<pair<int, int>> zidni;
for (int i=1; i<n+1; ++i){
for (int j=1; j<m+1; ++j){
if (v[i][j] == '#') continue;
for (int k=0; k<4; ++k){
if (v[i + ymov[k]][j + xmov[k]] == '#'){
zidni.push_back(make_pair(i, j));
break;
}
}
}
}
bfs_odzida(v, zidni);
dijkstra(v, start, cilj);
cout << bio[cilj.first][cilj.second] << "\n";
return 0;
}
Compilation message (stderr)
portals.cpp: In function 'void bfs_odzida(const std::vector<std::__cxx11::basic_string<char> >&, std::vector<std::pair<int, int> >&)':
portals.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i=0; i<fronta.size(); ++i){
| ~^~~~~~~~~~~~~~
portals.cpp: In function 'int main()':
portals.cpp:107:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for (int j=0; j<s.size(); ++j){
| ~^~~~~~~~~
# | 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... |