이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
//#define int long long
#define all(x) x.begin(), x.end()
#define endl '\n'
const int N=505;
const int INF=1e9;
int32_t main() {
LCBorz;
int n,m;cin>>n>>m;
vector<vector<char>> v(n+2,vector<char>(m+2,'#'));
auto id=[&](int a,int b){
return a*N+b;
};
auto valid=[&](int a,int b){
return v[a][b]!='#';
};
auto rev=[&](int id){
return make_pair(id/N,id%N);
};
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>v[i][j];
}
}
vector<int> adj[N*N];
for(int i=1;i<=n;i++){
int last=0;
for(int j=1;j<=m;j++){
if(!valid(i,j)){
last=j;
continue;
}
adj[id(i,j)].push_back(id(i,last+1));
}
last=m+1;
for(int j=m;j>=1;j--){
if(!valid(i,j)){
last=j;
continue;
}
adj[id(i,j)].push_back(id(i,last-1));
}
}
for(int i=1;i<=m;i++){
int last=0;
for(int j=1;j<=n;j++){
if(!valid(j,i)){
last=j;
continue;
}
adj[id(j,i)].push_back(id(last+1,i));
}
last=n+1;
for(int j=n;j>=1;j--){
if(!valid(j,i)){
last=j;
continue;
}
adj[id(j,i)].push_back(id(last-1,i));
}
}
vector<int> dx={0,0,-1,1},dy={1,-1,0,0};
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!valid(i,j))continue;
for(int k=0;k<4;k++){
int a=i+dx[k],b=j+dy[k];
if(!valid(a,b))continue;
adj[id(i,j)].push_back(id(a,b));
}
}
}
queue<int> q;
vector<int> dis(N*N,INF);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(v[i][j-1]=='#'||v[i][j+1]=='#'||v[i-1][j]=='#'||v[i+1][j]=='#'){
q.push(id(i,j));
dis[id(i,j)]=0;
}
}
}
while(q.size()){
int p=q.front();
q.pop();
for(int j:adj[p]){
if(dis[j]==INF){
dis[j]=dis[p]+1;
q.push(j);
}
}
}
priority_queue<pair<int,int>> pq;
vector dis1(N*N,INF);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(v[i][j]=='S'){
dis1[id(i,j)]=0;
pq.push({0,id(i,j)});
}
}
}
while(pq.size()){
auto [dis2,t]=pq.top();
pq.pop();
dis2=-dis2;
if(dis2!=dis1[t])continue;
for(int j:adj[t]){
int cost=dis1[t];
if(abs(j-t)==N||abs(j-t)==1)cost+=1;
else cost+=dis[t]+1;
if(dis1[j]>cost){
dis1[j]=cost;
pq.push({-dis1[j],j});
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(v[i][j]=='C'){
cout<<dis1[id(i,j)]<<endl;
}
}
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
portals.cpp: In function 'int32_t main()':
portals.cpp:20:10: warning: variable 'rev' set but not used [-Wunused-but-set-variable]
20 | auto rev=[&](int id){
| ^~~
# | 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... |