# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
659068 | ansgar | Mecho (IOI09_mecho) | C++17 | 222 ms | 16420 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define vb vector<bool>
#define vc vector<char>
#define vvc vector<vc>
#define vvb vector<vb>
#define si set<int>
#define mii map<int,int>
const int mod=1e9+7;
const int N=2e5+1;
const int LN=LLONG_MAX/10;
vvc L;
int n,k;
vvi Bees;
vvi Bear;
vpii BeesLoc;
bool inside(int y,int x){
return (y>=0 and y<n and x>=0 and x<n and L[y][x]!='T');
}
void bfsBees(){
queue<pii> Q;
for(auto B : BeesLoc){
Q.push(B);
Bees[B.first][B.second]=-1;
}
while(Q.size()){
auto B=Q.front();
int x=B.second;
int y=B.first;
Q.pop();
if(inside(y-1,x) and Bees[y-1][x]>Bees[y][x]+1){
Bees[y-1][x]=Bees[y][x]+1;
Q.push({y-1,x});
}
if(inside(y+1,x) and Bees[y+1][x]>Bees[y][x]+1){
Bees[y+1][x]=Bees[y][x]+1;
Q.push({y+1,x});
}
if(inside(y,x-1) and Bees[y][x-1]>Bees[y][x]+1){
Bees[y][x-1]=Bees[y][x]+1;
Q.push({y,x-1});
}
if(inside(y,x+1) and Bees[y][x+1]>Bees[y][x]+1){
Bees[y][x+1]=Bees[y][x]+1;
Q.push({y,x+1});
}
}
}
void bfsBear(int sy,int sx,int w){
queue<pii> Q;
Q.push({sy,sx});
Bear[sy][sx]=0;
while(Q.size()){
auto B=Q.front();
int x=B.second;
int y=B.first;
Q.pop();
if(inside(y-1,x) and Bear[y-1][x]>Bear[y][x]+1 and Bees[y-1][x]-(Bear[y][x]/k)>=w){
Bear[y-1][x]=Bear[y][x]+1;
Q.push({y-1,x});
}
if(inside(y+1,x) and Bear[y+1][x]>Bear[y][x]+1 and Bees[y+1][x]-(Bear[y][x]/k)>=w){
Bear[y+1][x]=Bear[y][x]+1;
Q.push({y+1,x});
}
if(inside(y,x-1) and Bear[y][x-1]>Bear[y][x]+1 and Bees[y][x-1]-(Bear[y][x]/k)>=w){
Bear[y][x-1]=Bear[y][x]+1;
Q.push({y,x-1});
}
if(inside(y,x+1) and Bear[y][x+1]>Bear[y][x]+1 and Bees[y][x+1]-(Bear[y][x]/k)>=w){
Bear[y][x+1]=Bear[y][x]+1;
Q.push({y,x+1});
}
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>k;
L=vvc(n,vc(n));
Bear=vvi(n,vi(n,LN));
Bees=vvi(n,vi(n,LN));
int sx,sy,ex,ey;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>L[i][j];
if(L[i][j]=='M')sy=i,sx=j;
if(L[i][j]=='D')ey=i,ex=j;
if(L[i][j]=='H')BeesLoc.emplace_back(i,j);
}
}
bfsBees();
int high=LN;
int low=0;
int res=-1;
while(high>=low){
int mid=(high+low)/2;
Bear=vvi(n,vi(n,LN));
bfsBear(sy,sx,mid);
if(Bear[ey][ex]==LN)high=mid-1;
else{
res=mid;
low=mid+1;
}
}
cout<<res;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |