루미 알고리즘

#1556플래티넘Hoppers

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

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

Problem

Joshua has built a factory in minecraft, which consists of a grid where each cell contains a so-called hopper. A hopper can store an item within it, and points towards another hopper that is located either directly above, below, to the left or to the right of it. Once per second, each hopper pushes the item they have inside themselves to the hopper they are pointing at if they have an item inside. Sometimes this can lead to a hopper having more than one item inside simultaneously. This obviously doesn't work, as all the items pushed into the hopper that second are destroyed.

Joshua wants a stable factory without a lot of collisions. He therefore gives you a proposal on how the items should be placed and how the factory should look, and now wants to know for each item if it will collide or keep cycling endlessly in the factory. For all the items that collide, he also wants to know where the collision occurred so he can then update his design. Write a program to help him!

(그림은 원문 저장소에서 볼 수 있습니다)

Figure 1: The factory in sample 1 before it has started and after one second. Note that three items collided in the middle square.

Input

The first line contains the integers R, C R, C (2 ≤ R · C ≤ 10^6), N (1 ≤ N ≤ 10^5), the number of rows and columns of the grid, and the number of items in the proposal.

The following R rows describe the layout of the factory. Each of these rows consists of a string of C characters. Each character in the grid indicates the direction the hopper in that square is pointing towards. The characters will either be 'v' (down), '<' (left), '^' (up), or '>' (right). It is guaranteed that each hopper points to another hopper.

Then follows N rows, each containing two integers P_r (0 ≤ P_r ≤ R-1) and P_c (0 ≤ P_c ≤ C-1), the row and column where the ith item starts. No hopper initially contains two or more items.

Output

For each item in the same order as in the input, print a line with “cycle” if the item never collides with anything, or “r c” if it collides with something in the hopper at row r and column c (0-indexed).

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
110R · C ≤ 1000, N ≤ 1000
235N ≤ 1000
315If a hopper is part of a cycle, then at most one other hopper points towards it.
425No hopper points upwards.
515No additional constraints.

예제

예제 1

입력
3 3 5
vvv
>v<
>>^
0 1
1 0
1 2
2 0
2 2
출력
1 1
1 1
1 1
cycle
cycle

예제 2

입력
1 2 2
><
0 0
0 1
출력
cycle
cycle

예제 3

입력
3 3 3
vvv
v<<
>^^
0 0
0 2
2 2
출력
cycle
1 2
1 2

출처 표기

출처: Programmeringsolympiaden 2023년 결선 · 원문 페이지 · 이용 조건 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). 루미 알고리즘은 위 단체들과 관계없는 별도의 서비스이며, 자료는 원문 그대로 싣습니다. 문제마다 출처와 이용 조건을 따로 표기합니다.