# 【題解】ZeroJudge f313: 2. 人口遷移

```#include <iostream>
using namespace std;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int G[51][51], G1[51][51];

int main() {
int R, C, k, m;
cin >> R >> C >> k >> m;
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
cin >> G[i][j];
}
}

for (int i = 0; i < m; i++) {
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
if (G[r][c] == -1) {
G1[r][c] = G[r][c];
} else {
G1[r][c] = G[r][c];
for (int j = 0; j < 4; j++) {
int nx = r + dx[j];
int ny = c + dy[j];
if (nx >= 0 && nx < R && ny >= 0 && ny < C && G[nx][ny] != -1) {
G1[r][c] -= G[r][c] / k;
G1[r][c] += G[nx][ny] / k;
}
}
}
}
}
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
G[r][c] = G1[r][c];
}
}
}
int mn = 1e9;
int mx = 0;
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
if (G[r][c] != -1) {
mn = min(mn, G[r][c]);
mx = max(mx, G[r][c]);
}
}
}
cout << mn << "\n";
cout << mx << "\n";
return 0;
}
```

Python code (credit: Amy Chou)

```dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]

R, C, k, m = map(int, input().split())
G = []
for i in range(R):
G.append(list(map(int, input().split())))

G1 = [[None for i in range(C)] for j in range(R)]
for _ in range(m):
for r in range(R):
for c in range(C):
if G[r][c] == -1:
G1[r][c] = G[r][c]
else:
G1[r][c] = G[r][c]
for i in range(4):
nx = r + dx[i]
ny = c + dy[i]
if nx >= 0 and nx < R and ny >= 0 and ny < C and G[nx][ny] != -1:
G1[r][c] -= G[r][c] // k
G1[r][c] += G[nx][ny] // k
#不能寫 G = G1，除非使用deepcopy
for r in range(R):
for c in range(C):
G[r][c]= G1[r][c]

mn = 1000000000
mx = 0
for r  in range(R):
for c in range(C):
if  G[r][c] != -1:
mn = min(mn, G[r][c])
mx = max(mx, G[r][c])
print(mn)
print(mx)

```