#DSU0000060. Sân trượt băng (Skate)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sân trượt băng (Skate)
Skate
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
A skating rink has rows and columns, with walls around its boundary. Every square is either ice (.) or ground (#). The skater may begin at rest on any square of the rink, including a ground square.
On each move, the skater chooses one of the four directions: north, south, east, or west. Once the skater starts moving, they continue straight until reaching a boundary wall or a ground square other than the square where that move started. The skater stops there and can then choose another direction. In particular, a ground square is not impassable: the skater can enter and stop on it.
A square is considered visited if the skater passes through it; stopping on the square is not necessary. The rink is called efficient if, from every possible starting square, there is a sequence of moves that visits every square on the rink.
Before play begins, you may change some ice squares . into ground squares #. Find the minimum number of changes required to make the rink efficient.
Input
The first line contains , followed by strings of . and #, each of length .
Output
Print the minimum number of cells to change.
Subtasks
- Subtask 1 (20 points): ; Every cell is either
.or#. - Subtask 2 (30 points): ; Every cell is either
.or#. - Subtask 3 (50 points): ; Every cell is either
.or#.
Examples
Input
3 9
.........
.........
.........
Output
1
Explanation
Initially, starting from , it is impossible to pass square . Changing just square from . to # produces:
.........
........#
.........
After this change, a route covering every square exists from any starting square. One change is sufficient and necessary.