Iceberg - MarisaOJ: Marisa Online Judge

Iceberg

Time limit: 1000 ms
Memory limit: 256 MB
A lake can be represented by a matrix $A$ of size $n \times m$: - `X` represents an iceberg. - `.` represents a normal area. After a day, icebergs that are adjacent to water will melt. For example: ``` ..... ..... ..X.. ..... .XXX. --- After a day ---> ..X.. .XX.. ..... XXX.. .X... ``` From a cell you can move to any of the 4 adjacent cells as long as it is not an iceberg. There are point denoted by letter `L`. Determine after how many days two points are connected. ### Input - The first line contains 2 integers $n,m$. - The next $n$ lines, each line contains a string of $m$ character. There are exactly 2 characters `L`. ### Output - Print the number of days after which two points are connected. ### Constraints - $1 \le n,m \le 1500$. ### Example Input: ``` 5 5 L.... XXXXX XXXXX XXXXX ....L ``` Output: ``` 2 ```
Topic
Graph
Shortest path
DSU
Rating 1500
Source SPOJ
Solution (0) Solution