이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
signed main(){
int r,c,n;
cin>>r>>c>>n;
int sx,sy,tx,ty;
cin>>sx>>sy>>tx>>ty;
vector <vector <char> > a(r+1,vector <char> (c+1));
vector <vector <int> > d(r+1,vector <int> (c+1,1e18));
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
cin>>a[i][j];
}
}
d[sx][sy]=0;
queue <pair <int,int> > q1;
queue < pair <pair <int,int>,pair <int,int> > > q2;
vector <int> X={0,0,1,-1};
vector <int> Y={1,-1,0,0};
q1.push({sx,sy});
while(!q1.empty() || !q2.empty()){
while(!q1.empty()){
int x=q1.front().ff;
int y=q1.front().ss;
q1.pop();
for(int i=0;i<4;i++){
if(x+X[i]>=1 && x+X[i]<=r && y+Y[i]>=1 && y+Y[i]<=c){
if(a[x+X[i]][y+Y[i]]=='.' && d[x+X[i]][y+Y[i]]>d[x][y]){
d[x+X[i]][y+Y[i]]=d[x][y];
q1.push({x+X[i],y+Y[i]});
}
else if(a[x+X[i]][y+Y[i]]=='#' && d[x+X[i]][y+Y[i]]>d[x][y]+1){
d[x+X[i]][y+Y[i]]=d[x][y]+1;
q2.push({{x+X[i],y+Y[i]},{0,0}});
}
}
}
}
while(!q2.empty()){
int x=q2.front().ff.ff;
int y=q2.front().ff.ss;
int dfx=q2.front().ss.ff;
int dfy=q2.front().ss.ss;
q2.pop();
if(dfx==n-1 or dfy==n-1)q1.push({x,y});
for(int i=0;i<4;i++){
if(x+X[i]>=1 && x+X[i]<=r && y+Y[i]>=1 && y+Y[i]<=c){
int dfxn=dfx+(X[i]!=0);
int dfyn=dfy+(Y[i]!=0);
if(dfxn<n && dfyn<n){
if(d[x+X[i]][y+Y[i]]>d[x][y]){
d[x+X[i]][y+Y[i]]=d[x][y];
q2.push({{x+X[i],y+Y[i]},{dfxn,dfyn}});
}
}
}
}
}
}
cout<<d[tx][ty]<<"\n";
}
/*
*/
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |