#include #include #include #include #include using namespace std; const int dr[4] = {1, 0, -1, 0}; const int dc[4] = {0, 1, 0, -1}; int h, w, q; vector< vector > a; void paint(int r, int c, int x) { vector< vector > painted(h, vector(w, false)); int base_color = a[r][c]; if (base_color == x) { return; } queue > que; que.push(make_pair(r, c)); while (!que.empty()) { auto pos = que.front(); que.pop(); a[pos.first][pos.second] = x; painted[pos.first][pos.second] = true; for (int i = 0; i < 4; i++) { int nr = pos.first + dr[i]; int nc = pos.second + dc[i]; if (nr < 0 || h <= nr || nc < 0 || w <= nc) { continue; } if (painted[nr][nc]) { continue; } if (a[nr][nc] != base_color) { continue; } que.push(make_pair(nr, nc)); } } } int main() { cin >> h >> w; a.assign(h, vector(w, 0)); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> a[i][j]; } } cin >> q; int r, c, x; for (int i = 0; i < q; i++) { cin >> r >> c >> x; paint(r-1, c-1, x); } for (int i = 0; i < h; i++) { cout << a[i][0]; for (int j = 1; j < w; j++) { cout << " " << a[i][j]; } cout << endl; } return 0; }