6738.Minimal Grid Path

Time Limit: 1s Memory Limit: 512MB

You are given an $n \times n$ grid whose each square contains a letter.

You should move from the upper-left square to the lower-right square. You can only move right or down.

What is the lexicographically minimal string you can construct?

Input Format(From the terminal/stdin)

The first line has an integer $n$ : the size of the grid.

After this, there are $n$ lines that describe the grid. Each line has $n$ letters between A and Z.

  • $1 \le n \le 3000$

Output Format(To the terminal/stdout)

Print the lexicographically minimal string.

Sample Input

Copy
4
AACA
BABC
ABDA
AACA
 \n
    \n
    \n
    \n
    \n

Sample Output

Copy
AAABACA
       \n
Source: CSES, Dynamic Programming, 3359

Submit

请先 登录

© 2025 FAQs