Hey Google, Drive!

시간 제한60초메모리 제한1024 MB

요약
명령이 남북과 동서를 각각 같은 확률로 뒤바꿀 수 있는 상황에서 어떤 시작-끝 쌍을 확률 1에 가깝게 도달할 수 있는지 판별한다.
난이도

어려움10점 중 8점

유형
BFS, 그래프, 확률, 구현
정답자
아직 제출이 없습니다

문제

The Google Assistant and Android Auto teams are collaborating on a new prototype car that can be driven via voice commands. The early prototype works through a phone connected to a car simulator. Unfortunately, one of the early testers dropped their phone in the toilet, damaging the microphone and making it harder to use the new feature. Since they do not want to miss out on the opportunity, they want your help to use it anyway.

The early prototype moves on a simple grid of R\mathbf{R} rows and C\mathbf{C} columns and only understands 44 very simple voice commands: north, south, east, and west. Each command makes the car try to move exactly one cell in the corresponding direction. Because of the microphone issues, however, the system may mishear and interchange north and south, and separately, east and west. That means that a command of north may make the car move north or south, a command of south may make the car move south or north, and similarly both commands east and west may make the car move east or west when issued. In all cases, both movement options can happen with equal probability (1/21/2).

The tester set up a driving grid such that each cell can contain either a wall, a hazard, or be empty. If a command would make the car move into a wall, or outside the grid, it does nothing instead. If a command makes the car move into a hazard, the car cannot execute any more commands.

The tester has marked some empty cells of the grid as interesting starts and others as interesting finishes. A pair of an interesting start and an interesting finish is drivable if there is a strategy to drive the car through voice commands from the start that makes it end at the finish with probability at least 1−10−101001 - 10^{-{10^{100}}}. A strategy can choose which command to issue and when to stop depending on the outcome of the previous commands. Notice that if the car moves into a hazard it stops moving, so it cannot make it to the finish. The tester wants your help finding the list of all drivable pairs.

입력

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case starts with a line containing two integers R\mathbf{R} and C\mathbf{C}, the number of rows and columns of the grid. Then, R\mathbf{R} lines follow containing a string of C\mathbf{C} characters each. The jj-th character on the ii-th of these lines G_i,j\mathbf{G\_{i,j}} represents the grid in the ii-th row and jj-th column as follows:

  • A period (.) represents an uninteresting empty cell.
  • A hash symbol (#) represents a cell containing a wall.
  • An asterisk (*) represents a cell containing a hazard.
  • An English lowercase letter (a through z) represents an empty cell that is an interesting start.
  • An English uppercase letter (A through Z) represents an empty cell that is an interesting finish.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is NONE if there are no drivable pairs. Otherwise, yy must be a series of 22 character strings separated by spaces, representing all drivable pairs with the start letter first and the finish letter second, in alphabetical order.

제한

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • G_i,j\mathbf{G\_{i,j}} is either a period (.), a hash symbol (#), an asterisk (*) or a lowercase or uppercase English letter, for all i,ji, j.
  • The set \\{\mathbf{G\_{i,j}} for all i, j\\} contains at least 11 lowercase and at least 11 uppercase English letter.
  • Each lowercase and uppercase letter appears at most once among all G_i,j\mathbf{G\_{i,j}}.

힌트

In Sample Case #1, simply repeating the west command until reaching the finish is a viable strategy. Each time there is a 1/21/2 probability of reaching the finish and a 1/21/2 probability of staying in the same place. Thus, the probability of not reaching the finish in 1010110^{101} or fewer steps is 2−10101<10−101002^{-10^{101}} \lt 10^{-10^{100}}.

In Sample Case #2 a similar strategy as in Sample Case #1 can be used to get the car from any position in the top row (1) to any other with probability as high as desired, and similarly for all non-wall positions in the third row from the top (2). Analogously, but using the south command, the car can move between non-wall positions on the third column from the left (3). From both a and c we can use (1) to get to the third column from the left, then (3) to get right next to Y and then (2) to get to Y making both aY and cY drivable. Notice, however, that safely using the north or south commands from the third row can only be done in the third column, or otherwise the car may go into a hazard. Therefore, there is no safe way to move the car from the third to the fourth row, making aX and cX not drivable. From b, however, the car can use a similar strategy to get to X, and from X the car can get to Y by using the north or south command repeatedly (and stop when reaching Y, never risking going into the hazard above). Finally, the finish Z is completely isolated, so it cannot be part of a drivable pair.

In Sample Case #3, every path from the interesting start to the interesting finish goes through a hazard, which makes the pair not drivable.

In Sample Case #4, only the interesting start d has a viable strategy to get to the finish F.

예제1

  1. 예제 1

    입력
    4
    1 2
    aZ
    4 4
    a..c
    **.*
    .Y.#
    bX#Z
    2 2
    a*
    *Z
    2 7
    a*bcd*.
    ...*F#.
    
    예상 출력
    Case #1: aZ
    Case #2: aY bX bY cY
    Case #3: NONE
    Case #4: dF