이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <fstream>
#define endl '\n'
#define int long long
#define mod 1000000007
using namespace std;
char aa[1001][1001];
int dist[1001][1001];
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
signed main(){
int r,c; cin >> r >> c;
priority_queue<pair<int,pair<int,int>>>d;
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++) {
cin >> aa[i][j];
dist[i][j] = 1e18;
if(aa[i][j] == 'S') {
dist[i][j] = 0;
d.push({0,{i,j}});
}
}
}
while(d.size()){
auto x = d.top();
d.pop();
if(x.first > dist[x.second.first][x.second.second]) continue;
int f = 0;
int k = x.second.first, j = x.second.second;
if(k + 1 < r and aa[k + 1][j] == '#') f = 1;
if(k - 1 >= 0 and aa[k - 1][j] == '#') f = 1;
if(j + 1 < c and aa[k][j + 1] == '#') f = 1;
if(j - 1 >= 0 and aa[k][j - 1] == '#') f = 1;
if(k == 0 or k == r - 1 or j == 0 or j == c - 1) f = 1;
if(f){
k--;
while(k >= 0){
if(aa[k][j] == '#'){
if(dist[x.second.first][x.second.second] + 1 < dist[k + 1][j]){
dist[k + 1][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k + 1][j]}, {k + 1,j}});
}
break;
}else if(k == 0){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j]){
dist[k][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j]}, {k,j}});
}
}
k--;
}
k = x.second.first + 1;
while(k < r){
if(aa[k][j] == '#'){
if(dist[x.second.first][x.second.second] + 1 < dist[k - 1][j]){
dist[k - 1][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k - 1][j]}, {k - 1,j}});
}
break;
}else if(k == r - 1){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j]){
dist[k][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j]}, {k,j}});
}
}
k++;
}
k = x.second.first;
j--;
while(j >= 0){
if(aa[k][j] == '#'){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j + 1]){
dist[k][j + 1] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j + 1]}, {k,j + 1}});
}
break;
}else if(j == 0){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j]){
dist[k][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j]}, {k,j}});
}
}
j--;
}
j = x.second.second + 1;
while(j < c){
if(aa[k][j] == '#'){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j - 1]){
dist[k][j - 1] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j - 1]}, {k,j - 1}});
}
break;
}else if(j == c - 1){
if(dist[x.second.first][x.second.second] + 1 < dist[k][j]){
dist[k][j] = dist[x.second.first][x.second.second] + 1;
d.push({{dist[k][j]}, {k,j}});
}
}
j++;
}
}
for(int i = 0; i < 4; i++){
int a = x.second.first + dx[i], b = x.second.second + dy[i];
if(a < 0 or b < 0 or a >= r or b >= c) continue;
if(aa[a][b] == '#') continue;
if(1 + x.first < dist[a][b]){
dist[a][b] = x.first + 1;
d.push({{x.first + 1}, {a,b}});
}
}
}
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
if(aa[i][j] == 'C'){
if(dist[i][j] == 1e18){
cout << -1;
}else{
cout << dist[i][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... |