Simulate K ants that bounce off the walls of a W by H field and reverse on head-on meetings for T seconds, then report each ant position and direction.
Medium6MathSortingNo attempts yetTime limit2sMemory limit256 MBA rectangular field is W centimeters wide and H centimeters tall, with one corner at (0,0) and the opposite corner at (W,H). Inside the field, K ants move along the coordinate grid lines at 1 centimeter per second. Each ant moves in one of four directions.
Ants are proud and never give way. When two ants meet head-on, both turn around on the spot and run back the way they came. Two ants moving perpendicular to each other ignore each other, even when they pass through the same point. An ant that reaches the boundary of the field also turns around and runs back.
Turning takes no time. If a head-on meeting or a boundary touch happens exactly at second T, report the direction the ant has after the turn.
You are given the starting position and direction of every ant. Find the position and direction of each ant after T seconds.
The first line contains four integers W, H, K, T (1≤W,H,K≤100, 1≤T≤109).
Each of the next K lines contains three integers Xi, Yi, Di, where (Xi,Yi) is the position of the i-th ant and Di is the direction it moves in (0<Xi<W, 0<Yi<H, 1≤Di≤4). Di=1 means the ant moves toward increasing X, Di=2 toward increasing Y, Di=3 toward decreasing X, and Di=4 toward decreasing Y.
Numbers on one line are separated by single spaces. No two ants start at the same point.
Print K lines, one for each ant, in the same order as the input. Each line holds three integers separated by single spaces: the coordinates of the ant after T seconds and the direction it moves in.