제출 #1072176

#제출 시각아이디문제언어결과실행 시간메모리
10721767againMecho (IOI09_mecho)C++17
90 / 100
404 ms12624 KiB
#include <bits/stdc++.h> using namespace std ; const int N = 801 ; int dx[]{0 , 0 , 1 , -1} ; int dy[]{1 , -1 , 0 , 0} ; int n , s ; pair <int , int> Honey , Home ; char a[N][N] ; bool inside(int x , int y) { return (x >= 0 && y >= 0 && x < n && y < n && (a[x][y] == 'G' || a[x][y] == 'M')) ; } bool val(int x , int y) { return x / s < y ; } int ok(int time) { int mn2[n][n] ; fill_n(&mn2[0][0] , n*n , 1e9) ; queue<pair <int , int>> q2 ; for(int i = 0 ; i < n ; i++) { for(int j = 0 ; j < n ; j++) { if(a[i][j] == 'H') { q2.push({i, j}) ; mn2[i][j] = 0 ; } } } while(q2.size()) { pair <int , int> t = q2.front() ; q2.pop() ; for(int i = 0 ; i < 4 ; i++) { int x = t.first + dx[i] ; int y = t.second + dy[i] ; if(inside(x , y) && mn2[t.first][t.second] + 1 < mn2[x][y]) { mn2[x][y] = mn2[t.first][t.second] + 1 ; q2.push({x, y}) ; } } } int mn[n][n] ; fill_n(&mn[0][0] , n * n , 1e9 ) ; queue <pair <int , int>> q ; mn[Honey.first][Honey.second] = 0 ; if(mn2[Honey.first][Honey.second] > time) q.push(Honey) ; while(!q.empty()) { pair <int , int> t = q.front() ; q.pop() ; for(int i = 0 ; i < 4 ; i++) { int x = t.first + dx[i] ; int y = t.second + dy[i] ; if(val(mn[t.first][t.second] + 1 , mn2[x][y] - time) && inside(x , y) && mn[t.first][t.second] + 1 < mn[x][y]) { mn[x][y] = mn[t.first][t.second] + 1 ; q.push({x , y}) ; } } } for(int i = 0 ; i < 4 ; i++){ int x = Home.first + dx[i] ; int y = Home.second + dy[i] ; if(inside(x , y) && val(mn[x][y] , mn2[x][y] - time)) return true ; } return false ; } int main() { cin >> n >> s ; for(int i = 0 ; i < n ; i++) { for(int j = 0 ; j < n ; j++) { cin >> a[i][j] ; if(a[i][j] == 'M') Honey = {i , j} ; if(a[i][j] == 'D') Home = {i , j} ; } } int l = 0 , r = n * n + 1 ; while(l <= r) { int m = (l + r) / 2 ; if(ok(m)) l = m + 1 ; else r = m - 1 ; } cout << l - 1 << endl ; }
#Verdict Execution timeMemoryGrader output
Fetching results...