# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
698910 | aedmhsn | Mecho (IOI09_mecho) | C++17 | 180 ms | 6368 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define A first
#define B second
#define MP make_pair
#define ms(a, x) memset(a, x, sizeof(a))
#define boost() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pld;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const int mxN=805;
char grid[mxN][mxN];
int dist[2][mxN][mxN];
queue <pii> q;
vector <pii> moves={{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
int n, s;
bool valid(int i, int j){
if(i >= 1 && i <= n && j >= 1 && j <= n && grid[i][j] != 'H' && grid[i][j] != 'T')
return true;
return false;
}
void bfs(){
while(!q.empty()){
pii u = q.front();
q.pop();
for(auto [x, y]:moves){
if(valid(u.A+x, u.B+y) && dist[0][u.A+x][u.B+y] == -1){
dist[0][u.A+x][u.B+y] = dist[0][u.A][u.B]+1;
q.push({u.A+x, u.B+y});
}
}
}
}
void bfs2(int rest){
while(!q.empty()){
pii u = q.front();
q.pop();
for(auto [x,y]:moves){
if(valid(u.A+x, u.B+y) && dist[1][u.A+x][u.B+y] == -1 && (dist[1][u.A][u.B]+1)/s < dist[0][u.A+x][u.B+y]-rest + (grid[u.A+x][u.B+y] == 'D')){
dist[1][u.A+x][u.B+y] = dist[1][u.A][u.B]+1;
q.push({u.A+x, u.B+y});
}
}
}
}
int main(){
ms(dist, -1);
cin >> n >> s;
pii ip, ep;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cin >> grid[i][j];
if(grid[i][j] == 'H'){
q.push({i, j});
dist[0][i][j]=0;
}
else if(grid[i][j] == 'M')
ip={i, j};
else if(grid[i][j] == 'D')
ep={i, j};
}
}
int st=0, en=1e9, mid, ans=-1;
bfs();
while(st<=en){
mid = (st+en)/2;
q.push(ip);
ms(dist[1], -1);
dist[1][ip.A][ip.B]=0;
bfs2(mid);
if(dist[1][ep.A][ep.B] != -1){
ans = mid;
st = mid+1;
}
else
en = mid-1;
}
//cout << dist[1][4][4] << " " << dist[0][4][4];
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |