This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
pii s, t;
int d;
void read() {
cin >> s.first >> s.second;
cin >> t.first >> t.second;
cin >> d;
}
int dist(pii s, pii t) {
return abs(s.first - t.first) + abs(s.second - t.second);
}
void solve() {
read();
int dd = d - dist(s, t);
if (dd == 0) {
cout << 0 << '\n';
} else if (dd < 0 || dd % 2 || dist(s, t) == 1) {
cout << "impossible\n";
} else {
vector<pair<int, int>> ans;
if (abs(s.first - t.first) > 1) {
int x = t.first + (s.first - t.first) / abs(s.first - t.first);
for (int i = min(s.second, t.second); i <= max(s.second, t.second); ++i) {
ans.emplace_back(x, i);
}
dd -= 2;
int mn = min(s.second, t.second) - 1;
int mx = max(s.second, t.second) + 1;
while (dd > 0) {
ans.emplace_back(x, mn);
ans.emplace_back(x, mx);
dd -= 2;
--mn;
++mx;
}
} else if (abs(s.second - t.second) > 1){
int y = t.second + (s.second - t.second) / abs(s.second - t.second);
for (int i = min(s.first, t.first); i <= max(s.first, t.first); ++i) {
ans.emplace_back(i, y);
}
dd -= 2;
int mn = min(s.first, t.first) - 1;
int mx = max(s.first, t.first) + 1;
while (dd > 0) {
ans.emplace_back(mn, y);
ans.emplace_back(mx, y);
dd -= 2;
--mn;
++mx;
}
} else {
ans.emplace_back(min(s.first, t.first), max(s.second, t.second));
ans.emplace_back(max(s.first, t.first), min(s.second, t.second));
dd -= 4;
if (dd < 0 || dd % 2) {
cout << "impossible\n";
return;
} else {
auto a1 = ans[0], a2 = ans[1];
int dx1 = 1, dx2 = 1;
{
auto tmp = a1;
tmp.first += dx1;
if (tmp == s || tmp == t) dx1 = -1;
tmp = a2;
tmp.first += dx2;
if (tmp == s || tmp == t) dx2 = -1;
}
while (dd > 0) {
a1.first += dx1;
a2.first += dx2;
ans.emplace_back(a1);
ans.emplace_back(a2);
dd -= 2;
}
}
}
cout << ans.size() << '\n';
for (auto &[a, b] : ans) cout << a << ' ' << b << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |