이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
// #define int long long
using namespace std;
const int N=1005;
char grid[N][N];
int dis[N][N];
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0};int x,y;
vector<pair<int,int>>adj[N][N];
void bfs(int r,int c){
queue<pair<int,int>>q;
for(int i=1;i<=x;i++){
for(int j=1;j<=y;j++){
dis[i][j]=-1;
}
}
dis[r][c]=0;
q.push({r,c});
while(!q.empty()){
pair<int,int>u=q.front();q.pop();
for(auto r:adj[u.first][u.second]){
if(dis[r.first][r.second]!=-1){
continue;
}
dis[r.first][r.second]=dis[u.first][u.second]+1;
q.push({r.first,r.second});
}
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin>>x;cin>>y;int stx;int sty;int enx;int eny;
memset(grid,'#',sizeof(grid));
for(int i=1;i<=x;i++){
for(int j=1;j<=y;j++){
cin>>grid[i][j];
if(grid[i][j]=='S'){
stx=i;sty=j;
}
if(grid[i][j]=='C'){
enx=i;eny=j;
}
}
}
for(int i=0;i<=x+1;i++){
for(int j=0;j<=y+1;j++){
if(grid[i][j]!='#'){
for(int r=0;r<4;r++){
int newx=nx[r]+i;int newy=ny[r]+j;
if(newx>=1&&newx<=x&&newy>=1&&newy<=y&&grid[newx][newy]!='#'){
adj[i][j].push_back({newx,newy});
}
}
}
if(grid[i][j]=='#'){
int l=i-1;
if(i!=0){
//up
while(l!=0){
if(grid[l-1][j]=='#'){
break;
}
l--;
}
if(l!=i-1&&i!=0){
adj[i-1][j].push_back({l,j});
}
}
//down
l=i+1;
while(l!=x+1){
if(grid[l+1][j]=='#'){
break;
}
l++;
}
if(l!=i+1){
adj[i+1][j].push_back({l,j});
}
//right
l=j+1;
while(l!=y+1){
if(grid[i][l+1]=='#'){
break;
}
l++;
}
if(l!=j+1){
adj[i][j+1].push_back({i,l});
}
//left
l=j-1;
while(l!=0){
if(grid[i][l-1]=='#'){
break;
}
l--;
}
if(l!=j-1&&j!=0){
adj[i][j-1].push_back({i,l});
}
}
}
}
bfs(stx,sty);
cout<<dis[enx][eny]<<endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
portals.cpp: In function 'int main()':
portals.cpp:108:27: warning: 'eny' may be used uninitialized in this function [-Wmaybe-uninitialized]
108 | cout<<dis[enx][eny]<<endl;
| ^
portals.cpp:108:27: warning: 'enx' may be used uninitialized in this function [-Wmaybe-uninitialized]
portals.cpp:107:12: warning: 'sty' may be used uninitialized in this function [-Wmaybe-uninitialized]
107 | bfs(stx,sty);
| ~~~^~~~~~~~~
portals.cpp:107:12: warning: 'stx' may be used uninitialized in this function [-Wmaybe-uninitialized]
# | 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... |