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

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

Stable Wall

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

요약
글자로 표시된 폴리오미노 벽에서 각 조각이 항상 아래에서 받쳐지도록 쌓는 순서를 구하고, 그런 순서가 없으면 -1을 출력한다.
난이도

보통10점 중 5점

유형
그래프, 위상 정렬, 구현
정답자
아직 제출이 없습니다

문제

Apollo is playing a game involving polyominos. A polyomino is a shape made by joining together one or more squares edge to edge to form a single connected shape. The game involves combining N polyominos into a single rectangular shape without any holes. Each polyomino is labeled with a unique character from A to Z.

Apollo has finished the game and created a rectangular wall containing R rows and C columns. He took a picture and sent it to his friend Selene. Selene likes pictures of walls, but she likes them even more if they are stable walls. A wall is stable if it can be created by adding polyominos one at a time to the wall so that each polyomino is always supported. A polyomino is supported if each of its squares is either on the ground, or has another square below it.

Apollo would like to check if his wall is stable and if it is, prove that fact to Selene by telling her the order in which he added the polyominos.

입력

The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the two integers R and C. Then, R lines follow, describing the wall from top to bottom. Each line contains a string of C uppercase characters from A to Z, describing that row of the wall.

출력

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is a string of N uppercase characters, describing the order in which he built them. If there is more than one such order, output any of them. If the wall is not stable, output -1 instead.

제한

  • 1 ≤ T ≤ 100.
  • 1 ≤ R ≤ 30.
  • 1 ≤ C ≤ 30.
  • No two polyominos will be labeled with the same letter.
  • The input is guaranteed to be valid according to the rules described in the statement.

힌트

In sample case #1, note that ZOMA is another possible answer.

In sample case #2 and sample case #3, the wall is not stable, so the answer is -1.

In sample case #4, the only possible answer is EDCBA.

예제1

  1. 예제 1

    입력
    4
    4 6
    ZOAAMM
    ZOAOMM
    ZOOOOM
    ZZZZOM
    4 4
    XXOO
    XFFO
    XFXO
    XXXO
    5 3
    XXX
    XPX
    XXX
    XJX
    XXX
    3 10
    AAABBCCDDE
    AABBCCDDEE
    AABBCCDDEE
    
    예상 출력
    Case #1: ZOAM
    Case #2: -1
    Case #3: -1
    Case #4: EDCBA