logo AlgoBeat OnlineJudge
登录 注册

#216758. [GKS 2020 #C] Stable Wall

内存限制:1024 MiB 时间限制:2000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

Apollo is playing a game involving polyominoes. 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 polyominoes into a single rectangular shape without any holes. Each polyomino is labeled with a unique character from to .

Apollo has finished the game and created a rectangular wall containing rows and 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 polyominoes 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 polyominoes.

输入格式

The first line of the input gives the number of test cases, . test cases follow. Each test case begins with a line containing the two integers and . Then, lines follow, describing the wall from top to bottom. Each line contains a string of uppercase characters from to , 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

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

样例输出 1

Case #1: ZOAM
Case #2: -1
Case #3: -1
Case #4: EDCBA

数据范围与提示

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.

Limits

.

.

.

No two polyominoes will be labeled with the same letter.

The input is guaranteed to be valid according to the rules described in the statement.

Test Set 1

.

Test Set 2

.