# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
674602 | sudheerays123 | Mecho (IOI09_mecho) | C++17 | 227 ms | 11976 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
const ll N = 800+5 , INF = 1e18 , MOD = 1e9+7;
ll sx,sy,ex,ey;
vector<ll> dx = {0,0,-1,1};
vector<ll> dy = {-1,1,0,0};
vector<vector<ll>> bee(N,vector<ll>(N,INF));
vector<vector<char>> grid(N,vector<char>(N));
ll n,s;
bool check(ll start){
vector<vector<ll>> t(n+5,vector<ll>(n+5,INF));
t[sx][sy] = start;
queue<tuple<ll,ll,ll>> q;
q.push(make_tuple(sx,sy,start));
while(!q.empty()){
tuple<ll,ll,ll> u = q.front();
q.pop();
ll a,b,w;
tie(a,b,w) = u;
if(t[a][b] != w) continue;
for(ll i = 0; i <= 3; i++){
ll x = a+dx[i];
ll y = b+dy[i];
if(x < 1 || x > n || y < 1 || y > n || grid[x][y] == 'T' || grid[x][y] == 'H') continue;
ll d = t[a][b]+1;
ll c = start+ceil(double(d-start)/double(s));
if(t[x][y] > d && c <= bee[x][y]){
t[x][y] = d;
q.push(make_tuple(x,y,t[x][y]));
}
}
}
return (t[ex][ey] != INF);
}
void solve(){
cin >> n >> s;
queue<tuple<ll,ll,ll>> q;
for(ll i = 1; i <= n; i++){
for(ll j = 1; j <= n; j++){
cin >> grid[i][j];
if(grid[i][j] == 'M'){
sx = i;
sy = j;
}
else if(grid[i][j] == 'H'){
q.push(make_tuple(i,j,0));
bee[i][j] = 0;
}
else if(grid[i][j] == 'D'){
ex = i;
ey = j;
}
}
}
while(!q.empty()){
tuple<ll,ll,ll> u = q.front();
q.pop();
ll a,b,c;
tie(a,b,c) = u;
if(c != bee[a][b]) continue;
for(ll t = 0; t <= 3; t++){
ll x = a+dx[t];
ll y = b+dy[t];
if(x < 1 || x > n || y < 1 || y > n || grid[x][y] == 'D' || grid[x][y] == 'T') continue;
if(bee[x][y] > c+1){
bee[x][y] = c+1;
q.push(make_tuple(x,y,c+1));
}
}
}
ll low = 0 , high = bee[sx][sy]-1;
ll ans = INF;
while(low <= high){
ll mid = (low+high)/2;
if(check(mid)){
ans = mid;
low = mid+1;
}
else high = mid-1;
}
if(ans == INF) ans = -1;
cout << ans;
}
int main(){
fast;
ll tc = 1;
// cin >> tc;
while(tc--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |