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 = 1005;
int bio[N][N];
int fmov[4] = {-1, 0, 1, 0};
int smov[4] = {0, 1, 0, -1};
void portalfill(const vector<string>& v, const pair<int, int>& curr, queue<pair<int, int>>& que, bool prvi){
for (int i=0; i<4; ++i){
for (int j=1; ; ++j){
pair<int, int> novi = make_pair(curr.first + fmov[i] * j, curr.second + smov[i] * j);
if (v[novi.first][novi.second] == '#'){
if (bio[novi.first - fmov[i]][novi.second - smov[i]] == -1){
que.push(make_pair(novi.first - fmov[i], novi.second - smov[i]));
}
break;
}
}
}
bool zid = 0;
for (int i=0; i<4; ++i){
int f = curr.first + fmov[i];
int s = curr.second + smov[i];
if (v[f][s] == '#') zid = 1;
}
if (!prvi && zid) return;
for (int i=0; i<4; ++i){
pair<int, int> sus = make_pair(curr.first + fmov[i], curr.second + smov[i]);
int br = bio[sus.first][sus.second];
if (br != -1 && br == bio[curr.first][curr.second] - 1){
portalfill(v, sus, que, false);
}
}
}
void bfs(const vector<string>& v, pair<int, int> start){
queue<pair<int, int>> que;
que.push(start);
for (int steps = 0; !que.empty(); ++steps){
queue<pair<int, int>> novi;
while (!que.empty()){
pair<int, int> curr = que.front();
que.pop();
if (bio[curr.first][curr.second] != -1) continue;
bio[curr.first][curr.second] = steps;
if (v[curr.first][curr.second] == 'C') return;
bool zid = 0;
for (int i=0; i<4; ++i){
pair<int, int> sus = make_pair(curr.first + fmov[i], curr.second + smov[i]);
if (v[sus.first][sus.second] != '#'){
novi.push(sus);
}
else{
zid = 1;
}
}
if (zid){
portalfill(v, curr, novi, true);
}
}
que = novi;
}
}
void reset(){
for (int i=0; i<N; ++i){
for (int j=0; j<N; ++j){
bio[i][j] = -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 red = "";
red += '#';
for (int j=0; j<m; ++j){
char c;
cin >> c;
red += c;
if (c == 'S'){
start.first = i+1;
start.second = j+1;
}
if (c == 'C'){
cilj.first = i+1;
cilj.second = j+1;
}
}
red += '#';
v.push_back(red);
}
v.push_back(nule);
bfs(v, start);
cout << bio[cilj.first][cilj.second] << "\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... |