이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
pii f, t;
cin >> f.first >> f.second;
f.second--;
f.first--;
cin >> t.first >> t.second;
t.first--;
t.second--;
vector<int> len(n);
vector<vector<int> > dist(n);
for (int i = 0; i < n; i++) {
cin >> len[i];
dist[i].resize(len[i] + 1, 1e9);
}
dist[f.first][f.second] = 0;
queue<pii> q;
q.push(f);
while (!q.empty()) {
pii curr = q.front(); q.pop();
vector<pii> nei;
pii lft = curr;
if (lft.second > 0)
lft.second--;
else if (lft.first != 0)
lft = pii(lft.first - 1, len[lft.first - 1]);
pii rght = curr;
if (rght.second < len[rght.first])
rght.second++;
else if (rght.first < n - 1)
rght.second = 0, rght.first++;
pii up = curr;
if (curr.first != 0)
{
up.first--;
up.second = min(up.second, len[up.first]);
}
pii down = curr;
if (curr.first != n - 1) {
down.first++;
down.second = min(down.second, len[down.first]);
}
nei = {up, down, rght, lft};
for (pii guy : nei) {
if (dist[guy.first][guy.second] != 1e9)
continue;
dist[guy.first][guy.second] = dist[curr.first][curr.second] + 1;
q.push(guy);
}
}
cout << dist[t.first][t.second] << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |