# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
470769 | Yazan_Alattar | Mecho (IOI09_mecho) | C++14 | 134 ms | 11588 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 1007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
queue < pair <int,int> > q;
pair <int,int> st, en;
char a[M][M];
int n, s, d[M][M], dist[M][M], res[M][M];
bool vist[M][M];
int main()
{
// ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> s;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> a[i][j];
if(a[i][j] == 'H') q.push({i, j}), d[i][j] = 1;
if(a[i][j] == 'D') en = {i, j};
if(a[i][j] == 'M') st = {i, j};
}
}
while(!q.empty()){
int x = q.front().S;
int y = q.front().F;
q.pop();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
if(a[ny][nx] == 'H' || a[ny][nx] == 'T' || d[ny][nx] || ny < 1 || nx < 1 || ny > n || nx > n) continue;
d[ny][nx] = d[y][x] + 1;
q.push({ny, nx});
}
}
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j){
// cout << d[i][j] << " ";
// }
// cout << endl;
// }
q.push(st);
dist[st.F][st.S] = 1;
while(!q.empty()){
int x = q.front().S;
int y = q.front().F;
q.pop();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
if(a[ny][nx] == 'H' || a[ny][nx] == 'T' || (dist[y][x]) / s >= d[ny][nx] - 1 || dist[ny][nx] || ny < 1 || nx < 1 || ny > n || nx > n) continue;
dist[ny][nx] = dist[y][x] + 1;
q.push({ny, nx});
}
}
// cout << endl;
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j) cout << dist[i][j] << " ";
// cout << endl;
// }
if(!dist[en.F][en.S]) return cout << -1 << endl, 0;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
res[i][j] = -1;
if(d[i][j] && dist[i][j]) res[i][j] = (d[i][j] - 1) - ((dist[i][j] - 2) / s + 1);
// cout << res[i][j] << " ";
}
// cout << endl;
}
priority_queue < pair < int, pair <int,int> > > pq;
pq.push({res[st.F][st.S], st});
vist[st.F][st.S] = 1;
while(!pq.empty()){
int x = pq.top().S.S;
int y = pq.top().S.F;
int cost = pq.top().F;
pq.pop();
// cout << x << " " << y << " " << cost << endl;
if(x == en.S && y == en.F) return cout << cost << endl, 0;
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
int ncost = min(cost, res[ny][nx]);
if(vist[ny][nx] || dist[y][x] >= dist[ny][nx] || ncost == -1 || ny < 1 || nx < 1 || ny > n || nx > n) continue;
vist[ny][nx] = 1;
pq.push({ncost, {ny, nx}});
}
}
return 0;
}
// Don't forget special cases. (n = 1?)
// Look for the constraints. (Runtime array? overflow?)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |