#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define MAX 202 pair co[MAX*MAX]; int cnt[MAX][MAX]; int recently[MAX*MAX]; int belong[MAX*MAX]; unordered_set mp[MAX*MAX]; int cl[MAX*MAX]; inline int root(int b) { if (belong[b] == -1) { return b; } belong[b] = root(belong[b]); return belong[b]; } void merge(int a, int b) { a = root(a); b = root(b); if (a == b) { return; } if (recently[a] > recently[b]) { } else { swap(a, b); } if (mp[a].size() < mp[b].size()) { swap(mp[a], mp[b]); } for (auto ite = mp[b].begin();ite != mp[b].end();ite++) { if (cl[root(*ite)] != cl[b]) { mp[a].insert(root(*ite)); } } mp[b].clear(); belong[b] = a; } int k[MAX][MAX]; int h; int w; int dx[] = { 0,0,1,-1 }; int dy[] = { 1,-1,0,0 }; unordered_set cp; int ope = 0; int want; void change(int b) { cl[b] = want; recently[b] = ope; cp.clear(); vector > V; for (auto ite = mp[b].begin();ite != mp[b].end();ite++) { if (cl[root((*ite))] == cl[b]) { V.push_back(make_pair(root((*ite)), b)); } else { cp.insert((root(*ite))); } } swap(mp[b], cp); for (int i = 0;i < V.size();i++) { merge(V[i].first, V[i].second); } if (root(b) != b) { exit(1); } } int main() { cin >> h >> w; int C = 0; for (int i = 0;i < h;i++) { for (int j = 0;j < w;j++) { scanf("%d", &k[i][j]); cnt[i][j] = C; C++; co[C - 1] = make_pair(i, j); cl[C - 1] = k[i][j]; } } memset(belong, -1, sizeof(belong)); for (int i = 0;i < h;i++) { for (int j = 0;j < w;j++) { for (int k1 = 0;k1 < 4;k1++) { int ii = i + dx[k1]; int jj = j + dy[k1]; if (ii >= 0 && jj >= 0 && ii < h&&jj < w&&k[i][j] == k[ii][jj]) { merge(cnt[ii][jj], cnt[i][j]); } else { mp[root(cnt[i][j])].insert(root(cnt[ii][jj])); } } } } int q; scanf("%d", &q); while (q--) { ope++; int a, b; scanf("%d%d", &a, &b); a--; b--; int x; scanf("%d", &x); int f = root(cnt[a][b]); if (cl[f] == x) { continue; } want = x; change(f); } for (int i = 0;i < h;i++) { for (int j = 0;j < w;j++) { int f = cl[root(cnt[i][j])]; if (j) { printf(" "); } printf("%d", f); } puts(""); } return 0; }