F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Heroes of Might and Magic

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 368    Accepted Submission(s): 183


Problem Description
After a very long journey and uncountable number of uphill battles, hero Raelag (who is the transformed Agrael, notice that "Raelag" is an anagram of "Agrael") finally find the map shows where the "Holy Cap" is. Now he is going to find the treasure.

The map is made up of squares of equal size which are arranged in r rows and c columns. At the beginning, Raelag is at the square Labeled 'S', and the "Holy Cap" is at the square Labeled 'T'. It is guarranteed that there is only one 'S' and only one 'T' in a map. Squares that labeled with '#' in the map means squares that can't be passed; '.' means roads that can be passed without any physical loss and squares that labeled with an integer i (1 ¡Ü i ¡Ü 9) means when Raelag pass through this square, he will suffer i points of physical loss. Squares that labeled with capital letter 'A', 'B', 'C', 'D' or 'E' means doors that can be passed only if Raelag has the right key (There are at most 5 kinds of doors in a map). The squares labeled with 'S', 'T' and other capital letters all can be passed with out physical loss. Raelag can move to squares only if that square share an edge with the square he is now in. Raelag can't move out of the map.
 

Input
The fist line of input file contains a single integer T (1 ¡Ü T ¡Ü 10) - the number of test cases. For each test case, in the first line their are three integers: r, c and k (1 ¡Ü r, c ¡Ü 50, 0 ¡Ü k ¡Ü 5), representing the hight, length of the map and the number of kinds of doors in the map. Then there are r lines giving the map. Then k lines follows giving the position of the keys to the door 'A', 'B', 'C', 'D', and 'E' in order.

The uperleft conner is row 1 and colomn 1.
 

Output
For each test case, output a single integer: the minimum points of physical lose Raelag needs to suffer to find the "Holy Cup". If he can't find the "Holy Cap" any way, output "-1".
 

Sample Input
2 3 3 1 S21 #A# 11T 1 3 3 5 2 S..A. ##.## .B.AT 3 1 1 5
 

Sample Output
6 -1
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-05-08 04:00:14, Gzip enabled