# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
535822 | SlavicG | Mecho (IOI09_mecho) | C++17 | 178 ms | 11456 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
#define int long long
const int N = 805;
char c[N][N];
int n, s;
bool ok(int i, int j) {
return (i >= 0 && j >= 0 && i < n && j < n && c[i][j] != 'T' && c[i][j] != 'H');
}
vector<vector<int>> bfs(vector<pair<int,int>> nodes, char forbidden) {
vector<vector<int>> dist(n, vector<int>(n, INT_MAX));
queue<pair<int,int>> q;
for(auto f: nodes) {
dist[f.first][f.second] = 0;
q.push({f.first, f.second});
}
while(!q.empty()) {
int i = q.front().first, j = q.front().second;
q.pop();
if(ok(i + 1, j) && c[i + 1][j] != forbidden && dist[i + 1][j] == INT_MAX) {
dist[i + 1][j] = dist[i][j] + 1;
q.push({i + 1, j});
}
if(ok(i - 1, j) && c[i - 1][j] != forbidden && dist[i - 1][j] == INT_MAX) {
dist[i - 1][j] = dist[i][j] + 1;
q.push({i - 1, j});
}
if(ok(i, j + 1) && c[i][j + 1] != forbidden && dist[i][j + 1] == INT_MAX) {
dist[i][j + 1] = dist[i][j] + 1;
q.push({i, j + 1});
}
if(ok(i, j - 1) && c[i][j - 1] != forbidden && dist[i][j - 1] == INT_MAX) {
dist[i][j - 1] = dist[i][j] + 1;
q.push({i, j - 1});
}
}
return dist;
}
vector<vector<int>> bees;
bool dfs(int startx, int starty, int val) {
queue<pair<int,int>> q;
q.push({startx, starty});
if(val > bees[startx][starty]) return false;
vector<vector<int>> dist(n, vector<int>(n, -1));
dist[startx][starty] = 0;
while(!q.empty()) {
int i = q.front().first, j = q.front().second;
q.pop();
if(c[i][j] == 'D') return true;
if(ok(i + 1, j) && dist[i + 1][j] == -1 && val + (dist[i][j] + 1 + s - 1) / s <= bees[i + 1][j]) {
dist[i + 1][j] = dist[i][j] + 1;
q.push({i + 1, j});
}
if(ok(i - 1, j) && dist[i - 1][j] == -1 && val + (dist[i][j] + 1 + s - 1) / s <= bees[i - 1][j]) {
dist[i - 1][j] = dist[i][j] + 1;
q.push({i - 1, j});
}
if(ok(i, j + 1) && dist[i][j + 1] == -1 && val + (dist[i][j] + 1 + s - 1) / s <= bees[i][j + 1]) {
dist[i][j + 1] = dist[i][j] + 1;
q.push({i, j + 1});
}
if(ok(i, j - 1) && dist[i][j - 1] == -1 && val + (dist[i][j] + 1 + s - 1) / s <= bees[i][j - 1]) {
dist[i][j - 1] = dist[i][j] + 1;
q.push({i, j - 1});
}
}
return false;
}
void solve() {
cin >> n >> s;
int startx, starty;
vector<pair<int,int>> f;
for(int i = 0;i < n; ++i) {
for(int j = 0;j < n; ++j) {
cin >> c[i][j];
if(c[i][j] == 'M') {
startx = i, starty = j;
}
if(c[i][j] == 'H') {
f.pb({i, j});
}
}
}
bees = bfs(f, 'D');
/*
for(int i = 0;i < n; ++i) {
for(int j = 0;j < n; ++j) {
cout << bees[i][j] << " ";
}
cout << "\n";
}
*/
int l = 0, r = 1e9;
int ans = 0;
while(l <= r) {
int mid = l + r >> 1;
if(dfs(startx, starty, mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |