IOI '02 - Yong-In, Korea
In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a well-deserved reputation, because the frogs jump through your rice paddy at night, flattening rice plants. In the morning, after noting which plants have been flattened, you want to identify the path of the frog which did the most damage. A frog always jumps through the paddy in a straight line, with every hop the same length:

Your rice paddy has plants arranged on the intersection points of a grid as shown in Figure-1, and the troublesome frogs hop completely through your paddy, starting outside the paddy on one side and ending outside the paddy on the other side as shown in Figure-2:

Many frogs can jump through the paddy, hopping from rice plant to rice plant. Every hop lands on a plant and flattens it, as in Figure-3. Note that some plants may be landed on by more than one frog during the night. Of course, you can not see the lines showing the paths of the frogs or any of their hops outside of your paddy – for the situation in Figure-3, what you can see is shown in Figure-4:

From Figure-4, you can reconstruct all the possible paths which the
frogs may have followed across your paddy. You are only interested in
frogs which have landed on at least
Your task is to write a program to determine the maximum number of
landings in any single frog path (where the maximum is taken over all
possible frog paths). In Figure-4 the answer is
Input Specification
Your program is to read from standard input. The first line contains two
integers
Output Specification
Your program is to write to standard output. The output contains one
line with a single integer, the number of plants flattened along a frog
path which did the most damage if there exists at least one frog path,
otherwise,
Sample Input 1
6 7
14
2 1
6 6
4 2
2 5
2 6
2 7
3 4
6 1
6 2
2 3
6 3
6 4
6 5
6 7
Sample Output 1
7
Diagram for Sample Input 1
Sample 1 corresponds to the diagrams above.
Sample Input 2
6 7
18
1 1
6 2
3 5
1 5
4 7
1 2
1 4
1 6
1 7
2 1
2 3
2 6
4 2
4 4
4 5
5 4
5 5
6 6
Sample Output 2
4
Comments