Afshung Pizza Delivery
Time limit1sMemory limit128 MB
Given an ASCII street map with rotating traffic lights at intersections, find the minimum travel time from S to D, or report impossible.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph, Simulation, BFS
- Solved
- No attempts yet
Problem
The Afshung-Pizza chain runs a door-to-door pizza delivery service in Hamedung, a district of Sildavya, and it needs your help to plan the fastest possible delivery. Every rider carries a GIS device that shows all of Hamedung's streets and computes a route to the customer. The Elyasung Company, which builds and maintains the device, wants you to reprogram it so that it returns an even faster route.
Hamedung is a rectangular district whose two-way streets are all rectilinear (axis-aligned). The device draws the map with text characters, using the legend below:
- Each
-(dash) is one kilometer of west-east street, and each|(pipe) is one kilometer of north-south street. - A
+marks a sharp 90-degree turn of zero length that has no traffic light. - A single digit (from
1to9) marks an intersection whose traffic light has timing . Every intersection is either three-way or four-way. Smarks an Afshung-Pizza branch (the start) andDmarks the delivery address (the destination).*characters mark the border of the district.
To keep the district safe and free of accidents, every traffic light shows exactly one green and the rest red (two red lights at a three-way intersection, three at a four-way). One direction stays green for minutes — that is, during for some — and during the next minutes the green moves to the next direction, rotating counterclockwise. The same rule holds at every intersection.
Time is zero at the moment the rider starts moving. At time zero every intersection is set so that only its southern light is green — or, if the intersection has no southern street, its northern light — while all of its other lights are red.
The rider can change direction only at a turn (+) or at an intersection (a digit). For example, in a pattern such as -|-, a rider moving horizontally can neither cross the pipe nor turn, because there is no intersection there.
Given the map, the branch S, and the destination D, write a program that finds the fastest route to deliver the pizza from S to D. Assume the following:
SandDeach replace a-or a|.SandDare not adjacent to any intersection or turn.SandDare not adjacent to each other.- Any two intersections and/or turns are at least one kilometer apart.
S,D,+, and every digit have zero length.- The rider travels at one kilometer per minute.
- A rider facing a green light may move in any direction (straight, left, or right); no movement is allowed while facing a red light.
- There are no traffic jams or other obstacles along the way.
Input
The first line contains an integer (), the number of test cases.
Each test case begins with a line containing two integers (), the number of map rows, and (), the number of map columns. The next lines each contain a string of length made up of the characters -, |, +, (space), *, S, D, and the digits 1-9. The total number of intersections and turns (+) is at most 100.
Output
For each test case, print one line containing the minimum number of minutes needed to drive from S to D, or the word impossible (in lowercase letters) if D cannot be reached.