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 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;
}
}
}
void bfs_odzida(const vector<string>& v, vector<pair<int, int>>& fronta){
for (int step = 0; !fronta.empty(); ++step){
vector<pair<int, int>> novi;
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 = novi;
}
}
vector<pair<int, int>> moguci_portal(const vector<string>& v, const pair<int, int>& point){
vector<pair<int, int>> 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.push_back(make_pair(y - ymov[i], x - xmov[i]));
break;
}
}
}
return ret;
}
void bfs(const vector<string>& v, const pair<int, int>& start, const pair<int, int>& cilj){
queue<pair<int, pair<int, int>>> que;
que.push(make_pair(0, start));
while (!que.empty()){
queue<pair<int, pair<int, int>>> novi;
while (!que.empty()){
pair<int, pair<int, int>> poz = que.front();
que.pop();
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 (bio[y][x] != -1) continue;
novi.push(make_pair(poz.first + 1, make_pair(y, x)));
}
vector<pair<int, int>> krajevi = moguci_portal(v, curr);
for (int i=0; i<krajevi.size(); ++i){
if (bio[krajevi[i].first][krajevi[i].second] != -1) continue;
novi.push(make_pair(poz.first + dozida[curr.first][curr.second] + 1, krajevi[i]));
}
}
que = novi;
}
}
int main(){
reset();
int n;
int 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);
bfs(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:22: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]
22 | for (int i=0; i<fronta.size(); ++i){
| ~^~~~~~~~~~~~~~
portals.cpp: In function 'void bfs(const std::vector<std::__cxx11::basic_string<char> >&, const std::pair<int, int>&, const std::pair<int, int>&)':
portals.cpp:73:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for (int i=0; i<krajevi.size(); ++i){
| ~^~~~~~~~~~~~~~~
portals.cpp: In function 'int main()':
portals.cpp:98:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | 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... |