# Teleport Out!

Problem Description

You are in a rectangular maze and you would like to leave the maze as quickly as possible. The maze is a rectangular grid of square locations. Some locations are blocked. Some other locations are exits. If you arrive at an exit location, you can immediately leave the maze.

You may walk one step at a time, onto one of the locations adjacent to your current location. Two locations are adjacent if they share a side. That is, you can only move one step North, South, East or West. Of course, you cannot step off the maze, and you cannot step onto a blocked location.

In addition, at any step, you may choose to use your teleport device. This device will send you to a random non-blocked location inside the maze with uniform probability (including, possibly, the one where you currently are standing!). If the device happens to send you onto a spot that is also an exit, then you leave the maze immediately. Hooray!

The only way to leave the maze is by moving onto an exit (either by teleport or walking), you may not walk off the boundary of the maze. Write a program to calculate the expected number of steps you need in order to leave the maze. Assume that you would choose your actions (movements and using teleport device) optimally in order to minimize the expected number of steps to leave the maze. Using the teleport device is considered one step.

Input

There will be multiple test cases. Each test case starts with a line containing two positive integers R and C ( R<=200, C<=200 ). Then, the next R lines each contain C characters, representing the locations of the maze. The characters will be one of the following:

E

: represents an exit, there will be at least one `E' in every maze. `

Y' in every maze.

Y

: represents your initial location, there will be exactly one

X

: represents a blocked location.

.

: represents an empty space.

You may move/teleport onto any location that is marked `E',`

Y' or `.'.

The end of input is marked by a line with two space-separated 0's.

Output

For each test case, print one line containing the minimum expected number of steps required to leave the maze, given that you make your choices optimally to minimize this value. Print your result to 3 decimal places. Do not print any blank lines between outputs.

Sample Input

2 1

E

Y

2 2

E.

.Y

3 3

EX.

XX.

..Y

3 3

EXY

.X.

...

0 0

Sample Output

1.000

2.000

6.000

3.250

- 点赞
- 写回答
- 关注问题
- 收藏
- 复制链接分享
- 邀请回答

*2*条回答

#### 为你推荐

- 关于捕获的坐标的计算方式，采用C语言怎么才能实现的，思路的步骤
- Golang
- erlang
- r语言
- 1个回答

- Lampyridae Teleportae
- rest
- numbers
- 搜索
- acm
- lines
- 2个回答

- Teleport Out!
- each
- lines
- random
- 2个回答