# 10157

> <https://www.acmicpc.net/problem/10157>

1. 간단한데 푸는데 오래걸린 DFS 문제 ( 왜 오래 걸렸는지는 뒤에 나옴..)
2. 최악의 경우 1억번까지 계산해야하기 때문에  DFS 를 재귀로 말고 반복문으로 구현하자 !
3. 인덱스 범위 넘어가거나 방문되어 있으면 방향 틀어줌. 순서는 상, 우, 하, 좌 순으로 반복
4. 해당 k에 도달하면 현재 위치를 문제의 인덱스에 맞춰서 ( 열, 행 ) 출력
   * 아니 인덱싱을 희한하게 한 것도 모자라서, 왜 ( 열, 행 ) 으로 나타냈을까. 심지어 TC 30% 가 행 = 열 인 경우로 해놔서 30% 까지 채점중이 떠서 디버깅할 때 파악 못 함.
   * 문제를 잘 읽자 가 맞긴 한데, 이 수준이 맞긴 한가 싶다.

```python
c, r = map(int,input().split())

# visited 개념으로 만듦
a = [[0 for _ in range(c)] for _ in range(r)]
total = c * r

k = int(input())

# 애초에 배정 못 한다면 먼저 0 출력
if k > total:
    print(0)
    exit()

# 상, 우, 하, 좌
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

d = 0
cnt = 1
stack = [(r - 1, 0)]

while True:
    x, y = stack.pop()

    # 방문 수행
    a[x][y] = 1

    if cnt == k:
        print(y + 1, r - x)
        exit()

    # 다음 갈 곳
    nx = x + dx[d]
    ny = y + dy[d]

    # 인덱싱 벗어나거나 방문되어있다면 방향 틀기
    if 0 > nx or r <= nx or 0 > ny or c <= ny or a[nx][ny] == 1:
        d = (d + 1) % 4
        nx = x + dx[d]
        ny = y + dy[d]

    cnt += 1
    stack.append((nx, ny))
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://pyohamen.gitbook.io/til/algorithm/boj/10157.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
