# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
309480 | sofapuden | Mecho (IOI09_mecho) | C++14 | 474 ms | 6556 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
int n, k;
bool ok = 0;
int mid;
pair<int,int> sta, en;
vector<string> G;
vector<vector<int>> V;
vector<vector<int>> vis;
queue<pair<pair<int,int>,int>> Q, Q2;
void search2(pair<pair<int,int>,int> now){
if(vis[now.f.f][now.f.s])return;
vis[now.f.f][now.f.s] = 1;
if(now.f.f != n-1){
if(V[now.f.f+1][now.f.s] > now.s/k+mid){
Q2.push({{now.f.f+1,now.f.s},now.s+1});
}
}
if(now.f.s != n-1){
if(V[now.f.f][now.f.s+1] > now.s/k+mid){
Q2.push({{now.f.f,now.f.s+1},now.s+1});
}
}
if(now.f.f != 0){
if(V[now.f.f-1][now.f.s] > now.s/k+mid){
Q2.push({{now.f.f-1,now.f.s},now.s+1});
}
}
if(now.f.s != 0){
if(V[now.f.f][now.f.s-1] > now.s/k+mid){
Q2.push({{now.f.f,now.f.s-1},now.s+1});
}
}
}
void search(pair<pair<int,int>,int> now){
if(now.f.f < 0 || now.f.s < 0 || now.f.f >= n || now.f.s >= n)return;
if(V[now.f.f][now.f.s] != -1)return;
V[now.f.f][now.f.s] = now.s;
Q.push({{now.f.f-1,now.f.s},now.s+1});
Q.push({{now.f.f+1,now.f.s},now.s+1});
Q.push({{now.f.f,now.f.s-1},now.s+1});
Q.push({{now.f.f,now.f.s+1},now.s+1});
}
int main(){
cin >> n >> k;
G.resize(n);
V.resize(n, vector<int> (n,-1));
for(auto &x : G)cin >> x;
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(G[i][j] == 'T')V[i][j] = -2;
if(G[i][j] == 'M')sta = {i,j};
if(G[i][j] == 'D'){en = {i,j};V[i][j] = 1000000;}
if(G[i][j] == 'H')Q.push({{i,j},0});
}
}
while(!Q.empty()){
auto x = Q.front();
Q.pop();
search(x);
}
//for(int i = 0; i < n; ++i){
//for(int j = 0; j < n; ++j){
//cout << V[i][j] << " ";
//}
//cout << "\n";
//}
int best = -1;
int hi = V[sta.f][sta.s]-1, lo = 0;
while(lo <= hi){
mid = (hi+lo)/2;
vis.clear();
vis.resize(n, vector<int> (n, 0));
Q2.push({sta,0});
ok = 0;
while(!Q2.empty()){
auto x = Q2.front();
Q2.pop();
if(x.f == en){ok = 1;break;}
search2(x);
}
Q2 = queue<pair<pair<int,int>,int>>();
if(ok){lo = mid+1;best = mid;}
else hi = mid-1;
}
cout << best << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |