# 16234-인구이동

> [www.acmicpc.net/problem/16234](https://www.acmicpc.net/problem/16234)

1. 인구이동을 실시한다.
2. 인구이동을 실시하는 와중에 인구이동이 이뤄진다면 swich = ' on ' 해준다.
3. 인구이동이 끝난 후 swich 가 ' on ' 이라면 cnt += 1 해주고 또 실시한다.

```python
def dfs(y,x):
    global n,l,r,cnt,swich
    # 상하좌우에 대해서 탐색
    for i in range(4):
        ny = y + dy[i]
        nx = x + dx[i]
        # 인덱싱 범위 안이고, 인구차이 수가 문제조건 해당이고, 방문하지 않았다면
        if 0 <= ny < n and 0 <= nx < n and l <= abs(arr[y][x] - arr[ny][nx]) <= r and not visited[ny][nx]:
            swich = 'on'
            locs.append((ny,nx))
            cnt += arr[ny][nx]
            visited[ny][nx] = True
            dfs(ny,nx)
    return

n,l,r = map(int,input().split())
arr = [list(map(int,input().split())) for _ in range(n)]

visited = [[False for _ in range(n)] for __ in range(n)]
dy = [-1,1,0,0]
dx = [0,0,-1,1]

swich = 'on'
rst = 0

while swich == 'on':
    swich = 'off'
    visited = [[False for _ in range(n)] for __ in range(n)]
    for y in range(n):
        for x in range(n):
            locs = []
            if not visited[y][x]:
                visited[y][x] = True
                cnt = arr[y][x]
                locs.append((y,x))
                dfs(y,x)

                for ny,nx in locs:
                    arr[ny][nx] = cnt//len(locs)
    if swich == 'on':
        rst += 1

print(rst)
```


---

# 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/16234.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.
