이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int bio[N][N];
int rowmov[4] = {-1, 0, 1, 0};
int colmov[4] = {0, 1, 0, -1};
struct koor{
int row;
int col;
};
void portalfill(const vector<vector<char>>& v, const koor& curr, queue<koor>& que){
for (int i=0; i<4; ++i){
for (int j=1; ; ++j){
koor novi;
novi.row = curr.row + rowmov[i] * j;
novi.col = curr.col + colmov[i] * j;
if (v[novi.row][novi.col] == '#'){
if (bio[novi.row - rowmov[i]][novi.col - colmov[i]] == -1){
koor k;
k.row = novi.row - rowmov[i];
k.col = novi.col - colmov[i];
que.push(k);
}
break;
}
}
}
bool zid = 0;
for (int i=0; i<4; ++i){
int r = curr.row + rowmov[i];
int c = curr.col + colmov[i];
if (v[r][c] == '#') zid = 1;
}
if (zid) return;
for (int i=0; i<4; ++i){
int r = curr.row + rowmov[i];
int c = curr.col + colmov[i];
if (bio[r][c] == bio[curr.row][curr.col] - 1){
koor k;
k.row = r;
k.col = c;
portalfill(v, k, que);
}
}
}
void bfs(const vector<vector<char>>& v, pair<int, int> start){
queue<koor> que;
koor k;
k.row = start.first;
k.col = start.second;
que.push(k);
for (int steps = 0; !que.empty(); ++steps){
queue<koor> novi;
while (!que.empty()){
koor curr = que.front();
que.pop();
if (bio[curr.row][curr.col] != -1) continue;
bio[curr.row][curr.col] = steps;
if (v[curr.row][curr.col] == 'C') return;
bool zid = 0;
for (int i=0; i<4; ++i){
koor sus;
sus.row = curr.row + rowmov[i];
sus.col = curr.col + colmov[i];
if (v[sus.row][sus.col] != '#'){
novi.push(sus);
}
else{
zid = 1;
}
}
if (zid){
portalfill(v, curr, novi);
}
}
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;
vector<char> nule;
for (int i=0; i<m+2; ++i){
nule.push_back('#');
}
vector<vector<char>> v;
v.push_back(nule);
pair<int, int> start;
pair<int, int> cilj;
for (int i=0; i<n; ++i){
vector<char> red;
red.push_back('#');
for (int j=0; j<m; ++j){
char c;
cin >> c;
red.push_back(c);
if (c == 'S'){
start.first = i+1;
start.second = j+1;
}
if (c == 'C'){
cilj.first = i+1;
cilj.second = j+1;
}
}
red.push_back('#');
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... |