#P10677. 『STA - R6』inkar-usi

    ID: 9884 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>模拟贪心洛谷原创O2优化洛谷月赛

『STA - R6』inkar-usi

Description

Given an n×mn \times m character matrix where some positions are obstacles (denoted as #), find a path starting from any cell (revisiting cells is allowed) such that its lexicographical order is maximized.

It can be proven that the answer is either finite or formed by infinite repetitions of a finite string SS. If the answer is finite, output it directly. If the answer is infinite, output its shortest repeating cycle.

Input Format

The first line contains two positive integers nn and mm.

The next nn lines each contain a string of length mm, describing the corresponding row of the matrix.

Output Format

Output a single string representing the answer.

3 3
###
#A#
###
A
3 4
####
#AB#
####
BA
3 4
####
#AA#
####
A

Hint

This problem uses subtask scoring.

Data Constraints:

  • Subtask 1 (20 points): All non-obstacle characters in the matrix are A.
  • Subtask 2 (30 points): n,m3n, m \le 3.
  • Subtask 3 (50 points): No additional constraints.

For all test cases, 1n,m1031 \le n, m \le 10^3, all non-obstacle characters are uppercase letters, and the matrix contains at least one non-obstacle cell.

Translation by DeepSeek R1