luogu#P5003. 跳舞的线 - 乱拐弯

跳舞的线 - 乱拐弯

Background

The initial facing direction of the line can be chosen by yourself.

You can write DP.

After playing with LCA, Imakf felt bored and opened DL.

Imakf: Assassin Legend died again at 70%70\%! I suddenly feel like quitting...

Imakf remembered that after playing DL for 11 year, getting stuck on "Garden", "Church", and "Chaos", he finally cleared them after months of grinding and was delighted, but now he is trapped by this Assassin Legend again, and tears welled up in his eyes. Through the tears, he suddenly saw the phone turn into pixels! Imakf was thrilled! Then he could write a program to clear it automatically, right?

But shortly after, disappointment returned...

Imakf: I don’t know how to write it!!

Problem Description

A line is on a grid map. It starts at (1,1)(1, 1) (top-left corner) and must reach (M,N)(M, N). It can only move down or right, and only on integer grid points.

Sometimes Imakf wants to show off, and sometimes he wants to be lazy, so he will give you the entire map. You need to tell him the maximum and minimum number of turns needed to reach the destination.

Input Format

The first line contains two positive integers MM and NN.

Lines 2M+12 \sim M+1 each contain NN characters. A # denotes an obstacle; otherwise, the cell is free.

Output Format

Output two positive integers maxmax and minmin. maxmax is the maximum number of turns, and minmin is the minimum number of turns. If the destination is unreachable, output -1.

5 5
oooo#
ooooo
#oo#o
o#ooo
oo#oo

7 2

5 5
oooo#
ooooo
#oo##
o#o#o
oo#oo

-1

Hint

Explanation for Sample 11:

The red route has the most turns, and the blue route has the fewest turns.


Explanation for Sample 22:

Unreachable, so output -1.


Constraints:

Test points NN MM
151 \sim 5 100\leq 100
676 \sim 7 =200=200 not specified
8108 \sim 10 not specified

For 100%100\% of the testdata, it is guaranteed that 10M,N100010 \le M, N \le 1000.

Thanks to @Iowa_BattleShip for pointing out the data error.

Translated by ChatGPT 5