Dice on a Board
Time limit1sMemory limit128 MB
Roll a die from the start cell to the target for face-matching bonuses, reporting Impossible when unreachable and Infinity when the score grows without bound.
- Level
Medium7 of 10
- Topics
- Shortest path, Graph
- Solved
- No attempts yet
Problem
You and your friends got bored of chess and backgammon, so you made a new game. It is a single player game played with one backgammon die (the singular of dice) on a board that looks like a chess board.
The board has rows and columns. Every cell is either empty or holds one digit from to . A die with the numbers to on its six faces sits on the starting cell, and the edges of its bottom face are aligned with the axes of the board. Your goal is to move it to the target cell.
Rows are numbered to from top to bottom, and columns to from left to right. Forward is the direction of decreasing row number, backward the direction of increasing row number, right the direction of increasing column number, and left the direction of decreasing column number.
The initial orientation of the die is given by a string , a permutation of the digits to . Its characters give the number on the right, left, forward, backward, top and bottom face, in that order.
The die moves under these rules.
- You move the die to one of its four adjacent cells by flipping it onto the face pointing that way. For example, if the current orientation is
136425and you move the die to the cell on its right, the right face becomes the bottom face in the new cell, so the orientation becomes256431. - Your score starts at . When you move the die, if the number on the new bottom face equals the number in the cell you just entered, your score increases by the sum of those two numbers, otherwise it decreases by the sum of those two numbers. Entering the target cell does not change your score.
- You can not leave the board.
- Once you leave the starting cell, you can not enter it again.
- Once you enter the target cell, you can not leave it.
- You can not enter a cell that holds no number. The target cell is the only exception.
Given the board, the starting cell, the target cell and the initial orientation of the die, find the maximum score you can end up with after moving the die from the starting cell to the target cell under these rules.
Input
The first line contains a single integer , the number of test cases (). The specifications of test cases follow.
Each test case is given in lines. The first line contains two integers and (), the number of rows and the number of columns of the board. The second line contains the string describing the initial orientation of the die on the starting cell. Each of the remaining lines contains characters, where the -th character of the -th line is the value of the cell in row , column . Each character is one of the following.
.is an empty cell.Sis the starting cell, which appears exactly once on the board.Tis the target cell, which appears exactly once on the board.- A digit from
0to9is the value written in that cell.
Output
For each test case, print one line containing one of these.
Impossibleif you can not reach the target cell from the starting cell.Infinityif your final score has no upper limit and you can raise it without end.- Otherwise, the maximum score you can get.