eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Cargo Carriage

Cargo Carriage

\includegraphics{https://static.e-olymp.com/content/4b/4bdf59a905607c75047fe164c00da48d21c1db9f.jpg} A seemingly straightforward task such as driving cargo from point \textbf{A} to point \textbf{B} can sometimes be very tricky! \textbf{GPS} systems can be very helpful for finding routes, but usually don’t take all things into 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 intersections (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: look figure. Adjacency on this map is defined as neighboring north, south, east, or west. The only symbols that appear on the map are the following: • A \textbf{#} character indicates a road cell that trucks can drive on. Roads are adjacent to at most two other road, intersection, or warehouse cells. • A single number \[\textbf{0}-\textbf{9}\] marks an intersection controlled by a traffic light. Intersections are adjacent to at least three road cells. Intersections are uniquely numbered sequentially: no number will appear unless all nonnegative integers less than it also appear on the map. The behavior of traffic lights will be described below. • Exactly one character \textbf{A} marks the location of the warehouse where your trucks start. • Exactly one character \textbf{B} marks the location of the warehouse where you’d like to ship your cargo to. • A \textbf{.} character is just grass. You cannot drive on these. You have one cargo truck that starts at warehouse A, and you are trying to drive it to warehouse B according to the rules below. For simplicity, we can also discretize time into atomic units, or turns. • On a single turn, you may move the truck onto an adjacent road, intersection, or warehouse cell, or simply remain in the same cell. • A truck may only move into an intersection cell if the traffic light for the intersection is green in the direction the truck is entering from during that turn. However, a truck on an intersection cell can exit in any direction at any time. 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 indicating the east-west and north-south periods, respectively. For example, an intersection initially green on the north-south direction described by "\textbf{2 3}" will have a green light facing north and south on turns \textbf{1}-\textbf{3} inclusive, facing east and west on turns \textbf{4}-\textbf{5}, and again north and south on turns \textbf{6}-\textbf{8}, etc. \InputFile The input test file will contain multiple cases. Each test case begins with a single line containing two integers, m and n, separated by spaces. The street map consists of m rows (east-west) and n columns (north-south) of grid cells (\textbf{2} <= \textbf{m}, \textbf{n} <= \textbf{20}). The next \textbf{m} lines contain \textbf{n} characters each, which describe the map using the symbols defined in the problem statement above. For each numbered intersection that appears in the map, in ascending order beginning with \textbf{0}, there will be a line of text with the intersection number followed by either a '\textbf{-}' or '\textbf{|}' character and two integers, \textbf{ai} and \textbf{bi} (\textbf{1} <= \textbf{ai}, \textbf{bi} <= \textbf{100}), the duration (in turns) of the east-west and north-south periods of the light, respectively. A '\textbf{-}' indicates that the traffic light is initially green in the east-west direction, while a '\textbf{|}' indicates that it is initially green in the north-south direction. A blank line separates input test cases, as seen in the sample input below. A single line with the numbers "\textbf{0 0}" marks the end of input; do not process this case. \textbf{Output} For each test case, your program should print one integer on a single line: the minimum number of turns it takes to drive your truck from warehouse \textbf{A} to warehouse \textbf{B}. If it is not possible to get to warehouse \textbf{B}, print a single word "\textbf{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 \textbf{2} until it can enter on turn \textbf{7}, when the light is green, and then reaches intersection \textbf{0} on turn \textbf{10}, wait again west of intersection \textbf{1} until turn \textbf{14}, and finally enters warehouse \textbf{B} at the end of turn \textbf{17}).
Ліміт часу 0.1 секунд
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
3 4
A##B
#..#
####

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

2 2
A.
.B

0 0
Вихідні дані #1
3
17
impossible