QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 256 MB Total points: 100

#256. Chuck's Challenge

الإحصائيات

Chuck's Challenge is a game in which a player navigates a maze consisting of various tiles in a grid. The player can encounter doors that may be unlocked using keys they acquire along the way. You must write a program to determine the minimum number of doors that must be opened to reach the exit of the maze.

Key

Tile Meaning Passable
> Entrance Yes
< Exit Yes
a-z Key Yes
A-Z Door See rules
. Ground Yes
+ Wall No
~ Unstable floor See rules

Rules

  • Tiles that are diagonal to each other are not considered to be adjacent (only up, down, left, and right).
  • The player begins the maze visiting the entrance tile.
  • The player may only visit passable tiles that are adjacent to the tile the player is currently visiting.
  • Doors are considered impassable until a tile with a matching key ('A' matches 'a', 'B' matches 'b', etc.) has been visited.
  • At first, unstable floors are considered passable; however, if the player leaves an unstable floor and visits any other type of tile, it becomes impassable.
  • When an unstable floor tile becomes impassable, all adjacent unstable floor tiles become impassable.
  • The external edge of the maze is an impassable wall.

Input

The first line of input will contain the number of test cases, $T$ ($1 \leq T \leq 50$). Each test case will begin with a line containing two integers R C ($4 \leq R, C \leq 50$). Following that will be a maze with $R$ rows and $C$ columns. Only the characters in the key will be present in the maze.

Output

Each test case will have a single line of output. Print the minimum number of doors that must be opened to reach the exit or print "Impossible" if the exit cannot be reached.

Sample Input

2
8 15
+++++++++++++++
+>............+
+...a...+~+~+B+
+~+++++++~+~+.+
+~++..b..C+~+.+
+~++A++++++~+.+
+....~~~~~~c+<+
+++++++++++++++
8 6
++++++
+>.A<+
+.++++
+.+.a+
+~~..+
+~~..+
+~~..+
++++++

Sample Output

2
Impossible
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.