# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
491455 | XII | Mecho (IOI09_mecho) | C++17 | 169 ms | 10704 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORU(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define IOS cin.tie(0)->sync_with_stdio(false);
#define PROB "IOI09_mecho"
void Fi(){
if(fopen(PROB".inp", "r")){
freopen(PROB".inp", "r", stdin);
freopen(PROB".out", "w", stdout);
}
}
const int INF = 1e9;
const int N = 800 + 1;
int n, spd;
int a[N][N];
using pi = pair<int, int>;
pi S, E;
queue<pi> qe;
int d[N][N];
const int dx[] = {0, 0, -1, +1};
const int dy[] = {-1, +1, 0, 0};
void bfsBee(){
while(!qe.empty()){
auto [x, y] = qe.front(); qe.pop();
FOR(direc, 0, 4){
int i = x + dx[direc];
int j = y + dy[direc];
if(i < 1 || n < i || j < 1 || n < j) continue;
if(a[i][j] == 1 && d[i][j] == -1){
d[i][j] = d[x][y] + 1;
qe.emplace(i, j);
}
}
}
}
pi D[N][N];
bool bfsMecho(const int &T){
FORU(i, 1, n) FORU(j, 1, n) D[i][j] = {INF, INF};
qe.emplace(S); D[S.fi][S.se] = {T, 0};
while(!qe.empty()){
auto [x, y] = qe.front(); qe.pop();
// cout << x << " " << y << " " << D[x][y].fi << " " << D[x][y].se << "\n";
FOR(direc, 0, 4){
int i = x + dx[direc];
int j = y + dy[direc];
if(i < 1 || n < i || j < 1 || n < j) continue;
if(a[i][j] && D[i][j].fi == INF){
D[i][j] = D[x][y];
if(++D[i][j].se >= spd){
++D[i][j].fi;
D[i][j].se = 0;
}
if(a[i][j] == 2){
while(!qe.empty()) qe.pop();
return true;
}
if(D[i][j].fi < d[i][j]){
qe.emplace(i, j);
}
}
}
}
return false;
}
int main(){
IOS;
Fi();
cin >> n >> spd;
memset(d, -1, sizeof(d));
FORU(i, 1, n){
string s; cin >> s;
FORU(j, 1, n){
switch(s[j - 1]){
case 'T': a[i][j] = 0; break;
case 'G': a[i][j] = 1; break;
case 'M': a[i][j] = 1; S = {i, j}; break;
case 'D': a[i][j] = 2; E = {i, j}; break;
case 'H': a[i][j] = 0; qe.emplace(i, j); d[i][j] = 0; break;
}
}
}
bfsBee();
int l = 0, r = d[S.fi][S.se] - 1, ans = -1;
while(l <= r){
int m = (l + r) / 2;
if(bfsMecho(m)) l = m + 1, ans = m;
else r = m - 1;
}
cout << ans;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |