프로그램 명: stfd_cargo
제한시간: 1 초

A seemingly straightforward task such as driving cargo from point A t o point B can sometimes be very tricky! GPS systems can be very helpful for finding routes, but usually don’t take all things in to consideration. For instance, when finding the quickest route, do they consider waiting at traffic lights? What about congestion? Maybe you can write superior software? In this problem, you will be given a street map containing roads, numbered int ersections (with traffic lights), and two warehouses. Your task is to find the fastest route to drive a truck from warehouse A to warehouse B. Street maps are always drawn on a grid, and look like this:

#A##0##1#
.#..#..#.
.#..#..#.
.###2#.B.
Figure 2: A street map with warehouses, roads, and intersections.
Adjacency on this map is defined as neighboring north, south, east, or west. The only sym bols that appear on the map are the following: You have one cargo truck that starts at warehouse A, and you are trying to dr ive it to warehouse B according to the rules below. For simplicity, we can also discretize time into atomic units, or turns. Intersections with traffic lights periodically allow either east-west or north-south traffic flow, but not both at the same time. They are described by an initial direction and two numbers indica ting the east-west and north-south periods, respectively. For example, an intersection initially green on the north-south direction described by “2 3” will have a green light facing north and south on turns 1-3 inclusive, facing east and west on turns 4-5, and again north and south on turns 6-8, etc

입력

출력

Your program should print one integer on a single line: the minimum number of turns it takes to drive your truck from warehouse A to warehouse B. If it is not possible to get to warehouse B, print a single word “impossible”.

입출력 예

입력

3 4
A##B
#..#
####

출력

3

입력

4 9
#A##0##1#
.#..#..#.
.#..#..#.
.###2#.B.
0 - 1 17
1 | 3 5
2 - 2 4

출력

17

입력

2 2
A.
.B

출력

impossible
(In the second example, it is actually quicker to take the bottom route than the top. However, your truck must wait west at intersection 2 until it can enter on turn 7, when the light is green, and then reaches intersection 0 on turn 10, wait again west of intersection 1 until turn 14 , and finally enters warehouse B at the end of turn 17)
출처:standford/2008/

[질/답] [제출 현황] [푼 후(0)]
[ 채 점 ] [홈으로]  [뒤 로]