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.
Map each square to coordinates . What can one king move change about and ?
Unlike a rook, the king can fix horizontal and vertical difference in the same move. So think in terms of and , not Manhattan distance.
If and , one move can reduce each by at most . Therefore you need at least moves.
That lower bound is achievable: while both coordinates still differ, move diagonally toward the target, which reduces both differences by . Once one coordinate matches, keep moving straight in the other direction.
Simulate it directly. On each step, start with an empty string move; append R/L depending on the file and U/D depending on the rank. This automatically gives one of L, R, U, D, LU, LD, RU, RD, and the number of steps is exactly .
This is barely a shortest-path problem. Yes, you could BFS over squares, but that's like bringing a tank to a pillow fight.
Let the start be and the target be , where the letter is the -coordinate and the digit is the -coordinate.
In one king move, both coordinates can change by at most :
So if then any path needs at least moves, because a single move cannot reduce either difference by more than .
That lower bound is also achievable.
So the minimum number of moves is exactly
In metric-speak, Fancy name, tiny implementation.
Maintain the current square .
At each step:
R and do L and do U and do D and do The move string you build is automatically one of:
L, R, U, D, LU, LD, RU, RD.
Why does this work? Because every iteration decreases each nonzero coordinate difference by exactly . Therefore the value drops by exactly each time, until it becomes .
So after exactly the optimal number of steps, you're done. Clean, greedy, no nonsense.
Let
The simulation runs for exactly moves, so the complexity is which is at most on an board. Memory usage is besides the output itself.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
string s, t;
cin >> s >> t;
int x = s[0] - 'a';
int y = s[1] - '1';
int tx = t[0] - 'a';
int ty = t[1] - '1';
vector<string> ans;
while (x != tx || y != ty) {
string mv;
if (x < tx) {
mv += 'R';
++x;
} else if (x > tx) {
mv += 'L';
--x;
}
if (y < ty) {
mv += 'U';
++y;
} else if (y > ty) {
mv += 'D';
--y;
}
ans.push_back(mv);
}
cout << ans.size() << '\n';
for (const string& mv : ans) {
cout << mv << '\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();
string s, t;
cin >> s >> t;
int x = s[0] - 'a';
int y = s[1] - '1';
int tx = t[0] - 'a';
int ty = t[1] - '1';
vector<string> ans;
while (x != tx || y != ty) {
string mv;
if (x < tx) {
mv += 'R';
++x;
} else if (x > tx) {
mv += 'L';
--x;
}
if (y < ty) {
mv += 'U';
++y;
} else if (y > ty) {
mv += 'D';
--y;
}
ans.push_back(mv);
}
cout << ans.size() << '\n';
for (const string& mv : ans) {
cout << mv << '\n';
}
}