#D8143. Apple Adventure
Apple Adventure
Apple Adventure
Apple adventure
square1001 and E869120 got lost in the grid world of rows and rows!
Said the god of this world.
"When you collect apples and they meet, you'll be back in the original world."
Upon hearing this word, square1001 decided to collect more than of apples and head to the square where E869120 is.
Here, each cell in the grid is represented as follows.
's': square1001 This is the square where you are.
'e': E869120 This is the square where you are.
'a': A square with one apple on it. You can get an apple the first time you visit this trout. There are no more than 20 squares on this grid.
'#': It's a wall. You cannot visit this square.
'.': A square with nothing. You can visit this square.
square1001 You try to achieve your goal by repeatedly moving from the square you are in to the squares that are adjacent to each other up, down, left, and right. However, you cannot get out of the grid.
square1001 Find the minimum number of moves you need to achieve your goal.
However, it is assumed that E869120 will not move. Also, square1001 shall be capable of carrying or more of apples.
If the goal cannot be achieved, output "-1".
input
Input is given from standard input in the following format.
Let be the characters in the square from the top of the grid and the square from the left.
output
square1001 Find the minimum number of moves you need to reach your goal. However, if this is not possible, output "-1".
However, insert a line break at the end.
Constraint
- are integers.
- is one of's','e','a','#','.'.
- The grid contains only one's' and one'e'.
- The number of'a'in the grid is greater than or equal to and less than or equal to .
Input example 1
5 5 2 s .. # a . # ... a # e. # ... # a . # ...
Output example 1
14
Input example 2
7 7 3 ....... .s ... a. a ## ... a .. ### .. .a # e # .a . ### .. a .. # .. a
Output example 2
-1
If the purpose cannot be achieved, output "-1".
Input example 3
12 12 10 . ##### ...... .## ..... # ... .... a.a # a .. # . # .. # a ...... ..... a # s .. ..a ###. ##. # .e #. #. #. # A .. .. # a # ..... #. .. ## a ...... .a ... a.a .. #. a .... # a.aa .. ... a. # ... # a.
Output example 3
30
Example
Input
5 5 2 s..#a .#... a#e.# ...#a .#...
Output
14
inputFormat
outputFormat
output "-1".
input
Input is given from standard input in the following format.
Let be the characters in the square from the top of the grid and the square from the left.
output
square1001 Find the minimum number of moves you need to reach your goal. However, if this is not possible, output "-1".
However, insert a line break at the end.
Constraint
- are integers.
- is one of's','e','a','#','.'.
- The grid contains only one's' and one'e'.
- The number of'a'in the grid is greater than or equal to and less than or equal to .
Input example 1
5 5 2 s .. # a . # ... a # e. # ... # a . # ...
Output example 1
14
Input example 2
7 7 3 ....... .s ... a. a ## ... a .. ### .. .a # e # .a . ### .. a .. # .. a
Output example 2
-1
If the purpose cannot be achieved, output "-1".
Input example 3
12 12 10 . ##### ...... .## ..... # ... .... a.a # a .. # . # .. # a ...... ..... a # s .. ..a ###. ##. # .e #. #. #. # A .. .. # a # ..... #. .. ## a ...... .a ... a.a .. #. a .... # a.aa .. ... a. # ... # a.
Output example 3
30
Example
Input
5 5 2 s..#a .#... a#e.# ...#a .#...
Output
14
样例
5 5 2
s..#a
.#...
a#e.#
...#a
.#...
14