# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
988359 | steveonalex | Mecho (IOI09_mecho) | C++17 | 215 ms | 8016 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int N = 900;
const int INF = 1e6 + 69;
int n, s;
bitset<N> grid[N];
int dis1[N][N], dis2[N][N];
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s;
vector<string> field(n);
for(int i = 0; i<n; ++i) cin >> field[i];
for(int i = 0; i<=n+1; ++i) grid[i][0] = grid[0][i] = grid[i][n+1] = grid[n+1][i] = 1;
for(int i = 0; i<n; ++i) for(int j = 0 ;j<n; ++j) if (field[i][j] == 'T' || field[i][j] == 'D'){
grid[i+1][j+1] = 1;
}
for(int i = 0; i<=n+1; ++i) for(int j = 0; j<=n+1; ++j) dis1[i][j] = INF;
deque<pair<int, int>> q;
for(int i = 0; i<n; ++i) for(int j = 0; j<n; ++j) if (field[i][j] == 'H') {
q.push_back({i + 1, j+1});
dis1[i+1][j+1] = 0;
}
while(q.size()){
pair<int, int> u = q.front(); q.pop_front();
for(int dir = 0; dir < 4; ++dir){
pair<int, int> v = {u.first + dx[dir], u.second + dy[dir]};
if (!grid[v.first][v.second] && minimize(dis1[v.first][v.second], dis1[u.first][u.second] + 1)){
q.push_back(v);
}
}
}
pair<int, int> S, E;
for(int i = 0; i<n; ++i) for(int j = 0; j<n; ++j){
if (field[i][j] == 'D') E = {i+1, j+1};
if (field[i][j] == 'M') S = {i+1, j+1};
}
grid[E.first][E.second] = 0;
ll l = -1, r = 2 * n * n;
minimize(r, dis1[S.first][S.second] * s - 1);
while(l < r){
ll mid = (l + r + 1) >> 1;
for(int i = 0; i<=n+1; ++i) for(int j = 0; j<=n+1; ++j) dis2[i][j] = INF;
dis2[S.first][S.second] = mid;
deque<pair<int, int>> q; q.push_back(S);
while(q.size()){
pair<int, int> u = q.front(); q.pop_front();
int cur = dis2[u.first][u.second];
for(int dir = 0; dir < 4; ++dir){
pair<int, int> v = {u.first + dx[dir], u.second + dy[dir]};
if (!grid[v.first][v.second] && dis1[v.first][v.second] > (cur + 1) / s && minimize(dis2[v.first][v.second], cur + 1)){
q.push_back(v);
}
}
}
if (dis2[E.first][E.second] == INF) r = mid - 1;
else l = mid;
}
if (l == n * n * 2) exit(1);
if (l >= 0) l /= s;
cout << l << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |