Chess Rook Move
Programming Basics · C++
TASK
Problem
Given two different chessboard squares `(x1,y1)` and `(x2,y2)`, print `YES` if a rook can move from the first to the second in one legal move; otherwise print `NO`.
EXAMPLE
Example
1 1 2 2
NO
LIMITS
Constraints
1 ≤ x1, y1, x2, y2 ≤ 8; the two squares are different.
LEARN
Theory for this problem
+
These exercises combine arithmetic, comparisons, and small branches. Keep each condition explicit and easy to verify.
Connection to “Chess Rook Move”: here, intermediate values are best kept in named variables so the formula stays readable and data types remain clear.
NEED HELP?
Hints
+
['Break the task into a few simple checks instead of compressing everything into a clever expression.', 'After solving “Chess Rook Move”, verify the algorithm on your own small example and on an allowed boundary case. Print only the required result with no extra text.']
ANSWER
Solution
+
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main() {
long long x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << ((x1==x2 || y1==y2)?"YES":"NO") << '\n';
return 0;
}