아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Shortest Circuit

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

요약
고정된 전선이 달린 노브 격자가 주어질 때, 두 끝 노브가 연결되도록 노브를 90도씩 돌리면서 총 회전 수를 최소로 하는 값을 구한다.
난이도

보통10점 중 6점

유형
BFS, 그래프, 시뮬레이션
정답자
아직 제출이 없습니다

문제

The scientists have also found several unknown machines on Atlantis. Unfortunately their circuitry has been altered over time and they no longer work. Before they can even figure out what these machines do they will need to fix the broken circuits as quickly and effectively as possible.

These circuit boards have knobs arranged in a square grid. Each knob has either one or two wires connected jutting out of it in different directions. The circuit will have two end-knobs consisting of a single wire and the remaining knobs with two wires each. Knobs will have to be rotated in order to complete the circuit between the two end knobs.

Each wire of the two-wired knobs are oriented North, East, South, or West, but not both in the same direction. There are six different possible configurations. Assigning values to the directions so that N=1, E=2, S=4, and W=8, we can map the knobs to the Nth character in the alphabet. Two-wired knobs will be mapped as follows:

      Directions   Dec Char
      N  E  S  W    #  
     |      
     o-   1  1  0  0  = 3   C

     |
     o    1  0  1  0  = 5   E
     |

     |
    -o    1  0  0  1  = 9   I

     o-   0  1  1  0  = 6   F
     |
			  
    -o-   0  1  0  1  = 10  J
			    

    -o    0  0  1  1  = 12  L
     |

The end-knobs in each circuit will be similarly mapped:

      Directions   Dec
      N  E  S  W    #  Char
     |
     o    1  0  0  0  = 1   A
     
     o-   0  1  0  0  = 2   B

     o    0  0  1  0  = 4   D
     |

    -o    0  0  0  1  = 8   H

Given an X by Y grid of circuit knobs, determine the fewest number of spins required to connect the two nodes. Any knob can spin, but only 90 degrees at a time.

입력

First line of input contains the number of test cases. For each test case, a Width (2 ≤ X ≤ 10) and Height (2 ≤ Y ≤ 10) of the circuit followed by Y lines containing X knobs each.

출력

Output is simply a single number for each test case representing the fewest number of individual 90 degree "spins" required to complete the circuit.

예제1

  1. 예제 1

    입력
    2
    4 2
    IJEH
    LEHE
    3 4
    BCC
    IIF
    JEE
    LCA
    
    예상 출력
    6
    4