Skip to main content
Back to problems
Codeforces
Medium
Math
Geometry
Arrays
Amr and Pins

Move a point by one unit in any of the four directions so that it stays within a given distance from a target point.

Acceptance 0%
Problem Statement

Problem

You are given a point (x,y)(x, y) and a target point (x0,y0)(x_0, y_0). In one move, you may shift the point by exactly 1 unit in one of the four cardinal directions: up, down, left, or right.

Find a move that results in a new position that is still within distance dd from the target point, where distance is measured using the Euclidean metric. In other words, after the move, the squared distance to the target should not exceed d2d^2.

If there are multiple valid moves, any one of them is acceptable.

Notes

  • The starting point itself may or may not already satisfy the condition.
  • You only need to output one valid new position.
  • It is guaranteed in the original problem that a solution exists.

Input Format

  • The first line contains an integer tt — the number of test cases.
  • Each test case contains four integers: xx, yy, x0x_0, and y0y_0, followed by the allowed distance dd.
  • The exact original input format may vary slightly by platform, but the task is to choose a valid adjacent point based on the given coordinates and distance.

Output Format

For each test case, output two integers — the coordinates of any position reachable in one move from (x,y)(x, y) that satisfies the distance constraint.

Constraints

  • 1t1 \le t is small to moderate in the original contest setting.
  • Coordinates and distance fit in standard 32-bit signed integers.
  • A valid answer is always guaranteed.
  • A move must change exactly one coordinate by ±1\pm 1.
Examples
Sample cases returned by the problem API.

Example 1

Input

1
0 0 1 1 2

Output

1 0

Explanation

Moving from (0,0)(0,0) to (1,0)(1,0) gives squared distance (11)2+(01)2=1(1-1)^2 + (0-1)^2 = 1, which is within 22=42^2=4.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.