루미 알고리즘

#1578플래티넘The Ice Puzzle

PO 2024 결선코딩출제 Joshua Andersson영어 원문이 문제가 어렵다면 — 배우는 순서 보기

영어 원문입니다. 원문의 수식·표를 글자로 옮겨 모양이 다를 수 있습니다. 정확한 내용은 원문 페이지에서 확인하세요. 원문 페이지 열기 (새 창)

Problem

Olle hates crosses. They remind him too much of getting a Wrong Answer verdict in Programmeringsolympiaden. When he arrived at Vallhamra Icehall and saw that there were K (2 ≤ K ≤ 20000) crosses placed on the ice, he became angrier than a raging bull.

Being the problem solver he is, Olle has come up with a plan to solve this disastrous situation. He intends to transform all crosses into circles. The ice rink can be represented as an N × M grid, where K squares have a cross, and the remaining N · M - K squares are empty. The rows of the grid are numbered from 0 to N-1 from top to bottom, and the columns are numbered from 0 to M-1 from left to right. When Olle is done, all squares that originally had crosses should be transformed to contain circles instead.

Initially, he is at the square marked with S. This square also contains a cross. In a move, he can start moving in one of the directions up, down, right, or left, without the ability to turn. He will continue in the same direction until he reaches a cross or circle and stops on that square. If there are no crosses or circles in the direction Olle is moving, he will collide with the wall, suffering an injury serious enough to force him to abandon his plan. Each time he reaches a cross, he stops completely and changes it from a cross to a circle. If he ever reaches a circle, he also stops completely and transforms it back into a cross in his blind rage. Since he doesn't have all day, he wants to find a sequence of moves containing at most 10^5 moves that transforms all crosses into circles. For extra style points, he also wants to finish at the same square as he started, but this is not necessary in all test case groups.

Input

The first line of the input contains three integers N, M and R (2 ≤ N · M ≤ 10^6, R ∈ {0, 1}). The values N and M represent the number of rows and columns that make up the grid, and R indicates whether you must return to the starting square or not. If R = 1, you must return to the start, and if R = 0, you can end the sequence of moves at any square.

The following N lines each contain a string of length M, where the i-th of these (i = 0, 1, …, N-1) describes how the i-th row of the ice rink looks. Each character is either “.”, “S” or “X”. It is guaranteed that exactly one square contains “S” and that the number of “X” is equal to K - 1 (since the starting square is also a cross).

Output

If there exists a sequence of moves that transforms all crosses into circles, first output the length of any such sequence. The length can be at most 10^5. On the next line, output the sequence. It should consist of the characters v, <, ^, and >. v means that Olle should move downward, ^ upward, < to the left, and > to the right. If R=1, Olle must also finish on the same square as he started.

If there is no valid sequence of moves, output -1 and nothing else. Note that the existence of a valid sequence may depend on whether R=0 or R=1.

Scoring

Your solution will be tested on a set of test groups, each worth a number of points. Each test group contains a set of test cases. To get the points for a test group you need to solve all test cases in the test group.

GroupPointsConstraints
120R=0, K ≤ 100
25R=0, K ≤ 5000
315R=0
410R=1, K ≤ 100
530R=1, K ≤ 10000
620R=1

Explanation of Sample 1

Initially, Olle is on the cross in the upper-left corner, and the grid looks like this:

  X.X
  ...
  X.X

After the moves >, v and ^, Olle is located at the top-right cell of the grid, and it looks like this:

  X.X
  ...
  X.O

After having made the moves < and >, he is still located att at the top-right cell of the grid, and it looks like this:

  O.O
  ...
  X.O

Finally, he will make the moves <, v, ^ to create a cross-free ice rink, where he also finishes at the starting position (this is not a requirement in this test case, as R=0):

  O.O
  ...
  O.O

예제

예제 1

입력
3 3 0
S.X
...
X.X
출력
8
>v^<><v^

예제 2

입력
2 2 0
S.
.X
출력
-1

예제 3

입력
2 2 0
SX
X.
출력
3
><v

예제 4

입력
2 2 1
SX
X.
출력
-1

출처 표기

출처: Programmeringsolympiaden 2024년 결선 · 원문 페이지 · 이용 조건 CC BY-SA · LaTeX 원문을 화면용 글로 옮겼습니다(수식·표의 모양이 다를 수 있습니다). 내용은 고치지 않았습니다.

크리에이티브 커먼즈 저작자표시-동일조건변경허락.

문제 출처: 한국정보올림피아드(KOI) (CC BY-NC-SA 4.0) · International Olympiad in Informatics (IOI) (CC BY) · Centre for Education in Mathematics and Computing (CEMC), University of Waterloo (CC BY-NC 4.0) · Programmeringsolympiaden (CC BY-SA) · CSES Problem Set (Antti Laaksonen) (CC BY-NC-SA 4.0). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.