---
# System prepended metadata

title: f313. 2. 人口遷移

---

# f313. 2. 人口遷移
## 題目連結[f313](https://zerojudge.tw/ShowProblem?problemid=f313)

## 解題想法
* 創一個陣列move，用來儲存每個城市將會外移多少人口
* 再一一把每個城市的人口移動

## 程式碼
```python=
#input
r,c,k,days = map(int,input().split())
city = []
for i in range(r):
    city.append([int(i) for i in input().split()])

move = [[0 for i in range(c)] for i in range(r)]

#start
while days > 0:
    for i in range(r): #用move表示每個城市要移動多少人口
        for j in range(c):
            if city[i][j] != -1:
                move[i][j] = city[i][j] // k

    for i in range(r): #移動人口
        for j in range(c):
            if j < c-1 and city[i][j+1] != -1:
                city[i][j+1] += move[i][j]
                city[i][j] -= move[i][j]
            if j > 0 and city[i][j-1] != -1:
                city[i][j-1] += move[i][j]
                city[i][j] -= move[i][j]
            if i < r-1 and city[i+1][j] != -1:
                city[i+1][j] += move[i][j]
                city[i][j] -= move[i][j]
            if i > 0 and city[i-1][j] != -1:
                city[i-1][j] += move[i][j]
                city[i][j] -= move[i][j]           
    days -= 1

#最後找出最大&最小
big = 0
small = 999999
for i in range(r):
    if max(city[i]) > big:
        big = max(city[i])
        
    for j in range(c):
        if city[i][j] == -1: city[i][j] = 999999 #因為有些城市的數值是-1
    if min(city[i]) < small:
        small = min(city[i])

print(small)
print(big)
