Tractor
Time limit1sMemory limit128 MB
Given up to 50,000 hay bales on a 1000 by 1000 grid, find the fewest bales to remove so an axis-parallel path from the tractor's start to the origin exists.
- Level
Hard8 of 10
- Topics
- Graph, BFS, Binary search, Geometry
- Solved
- No attempts yet
Problem
After a long day of work, Farmer John forgot that he left his tractor in the middle of the field. His mischievous cows decide to play a prank on him: they drop bales of hay () at various spots in the field so that Farmer John cannot easily get the tractor out without first removing some of the bales.
The tractor's position and the positions of the hay bales are all points in the 2D plane with integer coordinates in the range to . No hay bale sits on the tractor's starting point. When Farmer John drives the tractor he may only move it parallel to the coordinate axes (north, south, east, west), and each move must be an integer number of units — for example, he might go units north and then units east. The tractor may never move onto a point occupied by a hay bale.
Determine the minimum number of hay bales Farmer John must remove so that he can drive the tractor to the origin of the plane.
Input
- Line 1: Three space-separated integers — the number of bales and the tractor's starting coordinates and .
- Lines 2 to : Each line gives the coordinates and of one hay bale.
Output
- Line 1: The minimum number of hay bales Farmer John must remove so the tractor can reach the origin .