# 3055 탈출

문제 : <https://www.acmicpc.net/problem/3055>

```python
R, C = map(int, input().split())
twMap = [list(input()) for _ in range(R)]
arrived = False
cnt = 0
dx, dy = [1,-1,0,0], [0,0,1,-1]

while(1):
    # 고슴도치 움직임
    moved = False
    for i in range(R):
        for j in range(C):
            if twMap[i][j] == 'S':
                for k in range(4):
                    if (0<=i+dx[k]<R)and(0<=j+dy[k]<C):
                        if twMap[i+dx[k]][j+dy[k]] == '.':
                            twMap[i+dx[k]][j+dy[k]] = -2
                            moved = True
                        elif twMap[i+dx[k]][j+dy[k]] == 'D':
                            arrived = True    
    for i in range(R):
        for j in range(C):
            if twMap[i][j] == -2:
                twMap[i][j] = 'S'

    # 티떱숲 홍수 갱신 (중복 계산 조심)
    for i in range(R):
        for j in range(C):
            if twMap[i][j] == '*':
                for k in range(4):
                    if (0<=i+dx[k]<R)and(0<=j+dy[k]<C):
                        if(twMap[i+dx[k]][j+dy[k]] == '.' or twMap[i+dx[k]][j+dy[k]] == 'S'):
                            twMap[i+dx[k]][j+dy[k]] = -1    
    for i in range(R):
        for j in range(C):
            if twMap[i][j] == -1:
                twMap[i][j] = '*'
    cnt += 1 

    if arrived:
        break
    elif not moved:
        arrived = False
        break

if arrived:
    print(cnt)
else:
    print('KAKTUS')
```

그냥 시뮬레이션이라고 생각하고 풀었는데

<https://wookcode.tistory.com/167>

시뮬레이션도 큐를 활용하면 필요한 부분만 갱신할 수 있다는 거 가져가기


---

# 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://lazyartisan.gitbook.io/note/main-page/algorithm/undefined-1/3055.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.
