이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
char arr[1003][1003];
bool been[1003][1003];
priority_queue<pair<int,pair<int,int> > > pq;
int dirx[4]={1,0,-1,0};
int diry[4]={0,1,0,-1};
int l[1003][1003],r[1003][1003],u[1003][1003],d[1003][1003];
int n,m;
bool check(int x,int y){
if(x<1 || x>n || y<1 || y>m)
return false;
if(been[x][y]==true)
return false;
return true;
}
void input(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>arr[i][j];
if(arr[i][j]=='S'){
pq.push({0,{i,j}});
}
}
}
}
void portals(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(arr[i][j]=='#'){
l[i][j]=j+1;
}
else if(j==1){
l[i][j]=1;
}
else{
l[i][j]=l[i][j-1];
}
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(arr[i][j] == '#'){
r[i][j] = j-1;
}
else if(j == m){
r[i][j] = m;
}
else{
r[i][j] = r[i][j+1];
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(arr[i][j]=='#'){
u[i][j]=i+1;
}
else if(i==1){
u[i][j]=1;
}
else{
u[i][j]=u[i-1][j];
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(arr[i][j]=='#'){
d[i][j]=i-1;
}
else if(i==n){
d[i][j]=n;
}
else{
d[i][j]=d[i+1][j];
}
}
}
}
int main(){
input();
portals();
while(!pq.empty()){
int w=-pq.top().first;
int x=pq.top().second.first;
int y=pq.top().second.second;
pq.pop();
if(arr[x][y]=='C'){
cout<<w<<endl;
return 0;
}
been[x][y]==true;
for(int i=0;i<4;i++){
int nx=x+dirx[i];
int ny=y+diry[i];
if(check(nx,ny)==true){
pq.push({-(w+1),{nx,ny}});
}
}
int ds=min({abs(l[x][y]-y),abs(r[x][y]-y),abs(u[x][y]-x),abs(d[x][y]-x)});
pq.push({-(ds+w+1), {x, l[x][y]} });
pq.push({-(ds+w+1), {x, r[x][y]} });
pq.push({-(ds+w+1), {u[x][y], y} });
pq.push({-(ds+w+1), {d[x][y], y} });
}
}
컴파일 시 표준 에러 (stderr) 메시지
portals.cpp: In function 'int main()':
portals.cpp:124:19: warning: statement has no effect [-Wunused-value]
124 | been[x][y]==true;
| ~~~~~~~~~~^~~~~~
# | 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... |