Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Doran moves like a king, so row-distance and column-distance can both shrink in the same Doran turn. That diagonal move is the whole scam.
Krug only gets real escape value from a coordinate where she is already on one side of Doran. If they share a row, moving vertically once just gets matched immediately; same idea for columns.
If Doran is below Krug (), Krug's row escape is upward. She has cells to spend before the top edge, plus the row gap . Total: turns.
The four possible escape clocks are: if Doran is below, if above, if to the right, and if to the left.
Take the maximum of those clocks. Do not add row and column times: Doran's diagonal movement makes those clocks run in parallel, not one after another.
Let Krug start at and Doran start at .
The big observation: Doran can move diagonally, so in one turn he can improve both row and column at the same time. Krug cannot. She only moves in one coordinate per turn. That is why this game collapses to a few distances to the border instead of some cursed minimax over a grid.
Useful escape directions
Krug can only build a real lead in a coordinate where she is already on one side of Doran.
For rows:
Columns are the same:
How long does one escape direction last?
Suppose . Doran is below Krug, so Krug's row escape is upward.
Krug can spend moves reaching row . After that, she cannot keep increasing the row distance. Doran still has to close the initial row gap . So the total row-based survival clock is
So if Doran is below Krug, candidate answer is .
Similarly:
Why maximum, not sum?
This is the common wrong turn.
Krug might have both a row escape and a column escape, but she cannot cash them in one after another for free. While Krug spends a move on one coordinate, Doran can move diagonally and handle both coordinates at once. The escape clocks run in parallel.
So Krug chooses the escape direction with the latest deadline, and Doran's diagonal chase prevents combining deadlines.
Therefore:
Directions whose condition is false are ignored.
There is never an infinite answer. The board is finite, and Doran's king movement eventually kills every escape route. Krug can stall; she cannot become immortal. Sad, but fair.
Algorithm
For each test case:
ans = 0.ans.Time complexity: per test case.
Memory complexity: .
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n, rK, cK, rD, cD;
cin >> n >> rK >> cK >> rD >> cD;
ll ans = 0;
if (rD > rK) ans = max(ans, rD);
else if (rD < rK) ans = max(ans, n - rD);
if (cD > cK) ans = max(ans, cD);
else if (cD < cK) ans = max(ans, n - cD);
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
ll n, rK, cK, rD, cD;
cin >> n >> rK >> cK >> rD >> cD;
ll ans = 0;
if (rD > rK) ans = max(ans, rD);
else if (rD < rK) ans = max(ans, n - rD);
if (cD > cK) ans = max(ans, cD);
else if (cD < cK) ans = max(ans, n - cD);
cout << ans << '\n';
}
}