시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
---|---|---|---|---|---|
2 초 | 512 MB | 74 | 8 | 6 | 10.714% |
In 21XX, the residents of the IOI planet plan to immigrate to a recently discovered planet.
The new planet has a field, which is a rectangular grid with R rows and C columns. The columns are parallel to the south-north direction, and the rows are parallel to the east-west direction. The cell in the i-th row from north and the j-th column from west is called the cell (i, j). The north-west corner of the field is the cell (1, 1), and the south-east corner is the cell (R,C). In each year, the residents of the IOI planet choose the direction of the wind blowing on the field. The direction is one of east, west, south, or north.
To engage in agriculture in the new planet, they will plant “JOI grasses” in all over the field in the new planet. In the spring of the first year of immigration, N cells in the field have JOI grasses.
The range of JOI grasses are expanded by the wind. Each summer, the seeds of JOI grasses are blown by the wind to the direction chosen by the residents of the IOI planet. The seeds move one cell to the direction of the wind, and they land on. If the seeds land on a cell of the field without JOI grasses, it will have JOI grasses in the spring of the next year. Once a cell has JOI grasses, it will keep JOI grasses in the future.
We want to calculate the minimum number of years until all the cells in the field have JOI grasses if we adjust the direction of the wind appropriately.
Write a program which calculates the minimum number of years until all the cells in the field have JOI grasses if we adjust the direction of the wind appropriately.
Read the following data from the standard input.
Write one line to the standard output. The output contains the minimum number of years until all the cells in the field have JOI grasses if we adjust the direction of the wind appropriately.
There are no additional constraints.
3 4 3 1 2 1 4 2 3
3
In Sample Input 1, the following cells have JOI grasses in the spring of the first year of immigration.
The field in the new planet; cells with ‘0’ have JOI grasses in the spring of the first year of immigration.
In this sample input, if we choose the directions of the wind for the first 3 years are west, south, south, then all the cells will have JOI grasses in the spring after 3 years. The numbers in the following table describe when each cell has JOI grasses in the spring. This is the minimum number of years.
4 4 4 1 1 1 4 4 1 4 4
4