

Time Limit: 3 sec / Memory Limit: 1024 MB
配点 : 点
問題文
アメンボのすぬけ君は南北 マス東西 マスの長方形の形をしたグリッド状の池に住んでいます。北から 番目、西から 番目のマスをマス とします。
いくつかのマスには蓮の葉が浮かんでおり、すぬけ君はそれらのマスには入ることができません。
が @
のときマス に蓮の葉が浮かんでいること、.
のときそうでないことを表します。
すぬけ君は一回水をかくことで東西南北のいずれかの方向に マス以上 マス以下移動することができます。 移動の途中に蓮の葉のあるマスがあってはいけません。また、蓮の葉のあるマスや池の外に移動することもできません。
すぬけ君がマス から まで移動するのに最小で何回水をかく必要があるか求めてください。 から まで移動することができない場合、そのことを指摘してください。
制約
- または
- は
.
または@
-
.
-
.
- 入力される数はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
出力
すぬけ君がマス から まで移動するのに必要な最小の水かきの回数を出力せよ。
から まで移動することができない場合、-1
を出力せよ。
入力例 1Copy
3 5 2 3 2 3 4 ..... .@..@ ..@..
出力例 1Copy
5
はじめ、すぬけ君はマス にいます。 以下のように 回水をかくことでマス まで移動することができます。
-
マス から西に マス進み、マス に移動する。
-
マス から北に マス進み、マス に移動する。
-
マス から東に マス進み、マス に移動する。
-
マス から東に マス進み、マス に移動する。
-
マス から南に マス進み、マス に移動する。
入力例 2Copy
1 6 4 1 1 1 6 ......
出力例 2Copy
2
入力例 3Copy
3 3 1 2 1 2 3 .@. .@. .@.
出力例 3Copy
-1
Score : points
Problem Statement
Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with east-west rows and north-south columns. Let be the square at the -th row from the north and -th column from the west.
Some of the squares have a lotus leaf on it and cannot be entered.
The square has a lotus leaf on it if is @
, and it does not if is .
.
In one stroke, Snuke can move between and squares (inclusive) toward one of the four directions: north, east, south, and west. The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.
Find the minimum number of strokes Snuke takes to travel from the square to . If the travel from to is impossible, point out that fact.
Constraints
- or .
- is
.
or@
. -
.
-
.
- All numbers in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the minimum number of strokes Snuke takes to travel from the square to , or print -1
if the travel is impossible.
Sample Input 1Copy
3 5 2 3 2 3 4 ..... .@..@ ..@..
Sample Output 1Copy
5
Initially, Snuke is at the square . He can reach the square by making five strokes as follows:
-
From , go west one square to .
-
From , go north two squares to .
-
From , go east two squares to .
-
From , go east one square to .
-
From , go south two squares to .
Sample Input 2Copy
1 6 4 1 1 1 6 ......
Sample Output 2Copy
2
Sample Input 3Copy
3 3 1 2 1 2 3 .@. .@. .@.
Sample Output 3Copy
-1