# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
58198 | gnoor | Mecho (IOI09_mecho) | C++17 | 256 ms | 9772 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
struct cell {
int r,c;
};
queue<cell> bee;
queue<cell> bear;
bool wall[1010][1010];
int disBear[1010][1010];
int disBee[1010][1010];
cell home;
cell start;
int di[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
int n,s;
bool check(int x) {
memset(disBear,63,sizeof(disBear));
disBear[start.r][start.c]=x*s;
if (disBee[start.r][start.c]<=x*s) return false;
bear.push(start);
int nowr,nowc,nextr,nextc;
while (!bear.empty()) {
nowr=bear.front().r;
nowc=bear.front().c;
bear.pop();
for (int i=0;i<4;i++) {
nextr=nowr+di[i][0];
nextc=nowc+di[i][1];
if (nextc<0||nextc>=n||nextr<0||nextr>=n) continue;
if (wall[nextr][nextc]) continue;
if (disBear[nextr][nextc]<=disBear[nowr][nowc]+1) continue;
if (disBee[nextr][nextc]<=(disBear[nowr][nowc]+1)/s) continue;
disBear[nextr][nextc]=disBear[nowr][nowc]+1;
bear.push(cell{nextr,nextc});
}
}
return disBear[home.r][home.c]!=0x3f3f3f3f;
}
int main () {
scanf("%d%d",&n,&s);
char x;
memset(disBee,63,sizeof(disBee));
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
scanf(" %c",&x);
if (x=='T') wall[i][j]=true;
else if (x=='M'){
start=cell{i,j};
}
else if (x=='D') home=cell{i,j};
else if (x=='H') {
bee.push(cell{i,j});
disBee[i][j]=0;
}
}
}
int nowr,nowc;
int nextr,nextc;
while (!bee.empty()) {
nowr=bee.front().r;
nowc=bee.front().c;
bee.pop();
for (int i=0;i<4;i++) {
nextr=nowr+di[i][0];
nextc=nowc+di[i][1];
if (nextc<0||nextc>=n||nextr<0||nextr>=n) continue;
if (wall[nextr][nextc]) continue;
if (nextr==home.r&&nextc==home.c) continue;
if (disBee[nextr][nextc]<=disBee[nowr][nowc]+1) continue;
disBee[nextr][nextc]=disBee[nowr][nowc]+1;
bee.push(cell{nextr,nextc});
}
}
int lo=0;
int hi=2*n;
int mid;
while (lo<hi) {
mid=(lo+hi+1)>>1;
if (check(mid)) {
lo=mid;
} else {
hi=mid-1;
}
}
if (check(lo)) printf("%d\n",lo);
else printf("-1\n");
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |