#K85327. Zigzag Apple Collection
Zigzag Apple Collection
Zigzag Apple Collection
You are given a rectangular garden of width W and height H containing N trees at distinct coordinates. A gardener can start at any one of these trees and then traverse the garden in a zigzag pattern for T minutes.
The traversal works as follows: If the gardener starts at a tree with coordinates \((s_x,s_y)\), then for every row \(y\) in the interval \([s_y, \min(s_y+T, H))\), the gardener visits all columns. For rows where \(y-s_y\) is even, the gardener traverses from left to right (i.e. from column 0 to \(W-1\)); for rows where \(y-s_y\) is odd, the direction is reversed (i.e. from column \(W-1\) to 0).
At each visited cell \((x,y)\), the gardener collects an apple if and only if that cell contains a tree. Note that the starting cell is always counted exactly once regardless of duplicate checks.
Your task is to find the maximum number of apples the gardener can collect by choosing the best starting tree.
Note: All formulas are given in \(\LaTeX\) format, e.g. the starting cell is \((s_x,s_y)\).
inputFormat
The input is given via standard input. The first line contains four space-separated integers: \(N\), \(W\), \(H\), and \(T\).
This is followed by \(N\) lines, each containing two integers \(x\) and \(y\), representing the coordinates of a tree.
outputFormat
Output a single integer via standard output: the maximum number of apples that can be collected following the described zigzag traversal.
## sample3 5 4 10
1 1
2 2
3 3
3
</p>