End of the Spiral
Time limit2sMemory limit128 MB
Given an N by M grid, simulate a spiral walk that starts east from the southwest corner and turns left whenever blocked, then output the final cell's coordinates.
- Level
Medium5 of 10
- Topics
- Simulation, Math, Implementation
- Solved
- No attempts yet
Problem
Sejun lives in a palace whose floor has N columns and M rows. He wants visitors to walk as much as possible before reaching him, so a spiral path is installed.
Visitors enter at the southwestern corner. They first keep moving east. Whenever the cell in front of them is outside the palace or has already been visited, they turn left and continue moving forward. The diagram below shows the visiting order for a palace with N = 6 and M = 4; letters are visited in alphabetical order.
nmlkji
oxwvuh
pqrstg
abcdef
Sejun wants to stay at the cell where this spiral walk ends. Write a program that prints the coordinate of that cell.
The southwestern corner is (0, 0), the southeastern corner is (N-1, 0), and the northeastern corner is (N-1, M-1).
Input
The first line contains two natural numbers N and M. Each is at most 5000.
Output
Print the coordinate x y of the cell where the spiral walk ends.