# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
916342 | codefox | Mecho (IOI09_mecho) | C++14 | 241 ms | 54352 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define piipi pair<pii, int>
int N = 1e6;
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n, s;
cin >> n >> s;
vector<string> grid(n+2);
string ns = "";
for (int i = 0; i < n+2; i++) ns += "T";
grid[0] = ns;
grid[n+1] = ns;
for (int i = 1; i <= n; i++)
{
cin >> grid[i];
grid[i] = "T" + grid[i] + "T";
}
vector<vector<int>> graph(N);
queue<pii> q;
int home = 0;
int mecho = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
int ind = i*(n+2) +j;
if (grid[i][j]=='H') q.push({0, ind});
if (grid[i][j]=='D') home = ind;
if (grid[i][j]=='M') mecho = ind;
if (grid[i][j-1]!='T') graph[ind].push_back(ind-1);
if (grid[i][j+1]!='T') graph[ind].push_back(ind+1);
if (grid[i+1][j]!='T') graph[ind].push_back(ind+n+2);
if (grid[i-1][j]!='T') graph[ind].push_back(ind-n-2);
}
}
vector<int> dist(N, -1);
while (q.size())
{
int i, d;
tie(d, i) = q.front();
q.pop();
if (dist[i]!=-1) continue;
dist[i] = d;
for (int ele:graph[i])
{
q.push({d+1, ele});
}
}
priority_queue<pii, vector<pii>, less<pii>> toadd;
vector<int> reach(N, 0);
toadd.push({dist[home], home});
for (int i = N-1; i >= 0; i--)
{
vector<int> nextadd;
while(toadd.size() && toadd.top().first >= i)
{
q.push({0, toadd.top().second});
toadd.pop();
while (q.size())
{
int j, d;
tie(d, j) = q.front();
q.pop();
if (reach[j]==1) continue;
reach[j] = 1;
for (int ele:graph[j])
{
if (dist[ele]>=i && d < s) q.push({d+1, ele});
else nextadd.push_back({ele});
}
}
}
for (int ele:nextadd)
{
if(!reach[ele]) toadd.push({dist[ele], ele});
}
if (reach[mecho])
{
cout << i-1;
return 0;
}
}
cout << -1;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |