#include #include #include #include #include #include #include #define MAXN 1024 using namespace std; typedef pair pii; int N, M; int t[MAXN][MAXN]; int def[MAXN][MAXN]; void read() { scanf("%d %d", &N, &M); for(int i = 0; i < M; i++) { int x, y, w; scanf("%d %d %d", &x, &y, &w); t[x][y] = w; def[x][y] = w; } } int cnt, currC; int startX, startY; int code[MAXN][MAXN]; int currCode = 0; bool marked[MAXN][MAXN]; int dirX[4] = {0, 0, 1, -1}; int dirY[4] = {1, -1, 0, 0}; void DFS(int x, int y) { if(x == 0) return; marked[x][y] = true; code[x][y] = currCode; t[x][y] = currC; cnt--; for(int i = 0; i < 4; i++) { int newX = x + dirX[i], newY = y + dirY[i]; if(newX > N || newX <= 0 || newY > N || newY <= 0) continue; if((!marked[newX][newY]) && (t[newX][newY] == currC)) { DFS(newX, newY); } } } void BFS(int sx, int sy) { queue Q; t[sx][sy] = currC; marked[sx][sy] = true; Q.push(pii(sx, sy)); cnt--; while(!Q.empty()) { int x = Q.front().first; int y = Q.front().second; Q.pop(); code[x][y] = currCode; for(int i = 0; i < 4; i++) { int newX = x + dirX[i], newY = y + dirY[i]; /*if(code[x][y] == 7) printf("%d %d ---> %d %d\n", x, y, newX, newY); */ if(newX > N || newX <= 0 || newY > N || newY <= 0) continue; if(t[newX][newY] == currC && !marked[newX][newY]) DFS(newX, newY); if((t[newX][newY] == 0) && cnt) { marked[newX][newY] = true; t[newX][newY] = currC; cnt--; Q.push(pii(newX, newY)); } } } } void unget(int sx, int sy) { queue Q; Q.push(pii(sx, sy)); t[sx][sy] = def[sx][sy]; marked[sx][sy] = true; while(!Q.empty()) { int x = Q.front().first; int y = Q.front().second; Q.pop(); code[x][y] = 0; for(int i = 0; i < 4; i++) { int newX = x + dirX[i], newY = y + dirY[i]; if(newX > N || newX <= 0 || newY > N || newY <= 0) continue; if((t[newX][newY] == currC) && !marked[newX][newY]) { t[newX][newY] = def[newX][newY]; marked[newX][newY] = true; Q.push(pii(newX, newY)); } } } } void print() { printf("\n"); for(int i = 1; i <= N; i++) { for(int j = 1; j <= N; j++) printf("%2d ", t[i][j]); printf("\n"); } printf("\n"); } void mark(int i, int j, int count) { memset(marked, false, sizeof(marked)); cnt = count; currC = count; startX = i; startY = j; currCode++; BFS(i, j); //print(); //printf("{%d, %d} (%d) ---> %d\n", i, j, count, currCode); if(cnt != 0) { memset(marked, false, sizeof(marked)); currCode--; cnt = count; unget(i, j); //print(); } } int used[MAXN]; int currP = 1; void solve() { // print(); for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) if(code[i][j] == 0 && t[i][j] != 0) mark(i, j, t[i][j]); /*for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) used[code[i][j]] = true; for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) while(used[code[i][j] - 1] != true && code[i][j] > 1) { code[i][j]--; used[code[i][j]] = true; used[code[i][j] + 1] = false; } */ for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) { if(used[code[i][j]] == 0 && code[i][j] != 0) used[code[i][j]] = currP++; code[i][j] = used[code[i][j]]; } for(int i = 1; i <= N; i++) { for(int j = 1; j <= N; j++) { if(t[i][j] != 0) printf("%d ", code[i][j]); else printf("0 "); /* //if(t[i][j] == 0) { printf("0 "); } else if(t[i][j] == t[i - 1][j]) { code[i][j] = code[i - 1][j]; printf("%d ", code[i][j]); } else if(t[i][j] == t[i][j - 1]) { code[i][j] = code[i][j - 1]; printf("%d ", code[i][j]); } else { code[i][j] = ++currCode; printf("%d ", code[i][j]); }*/ } printf("\n"); } } int main() { freopen("regions.in", "r", stdin); freopen("regions.out", "w", stdout); read(); solve(); return 0; }