thongtrikhuvuccode

 avatar
quoc14
c_cpp
a month ago
2.5 kB
3
Indexable
Never
caidat
#include <iostream>

using namespace std;

int n;
int a[105][105], new_a[105][105];

int visit0[105][105], visit1[105][105];
int cur;
int cur0;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int tanso[105];
int max_tanso;
int max_vung;
bool inside(int x, int y) {
	if (x >= 1 && x <= n && y >= 1 && y <= n) {
		return true;
	}
	return false;
}

struct point {
	int x;
	int y;
};

point xungquanh[105];
point zero[105];

void resetvisit1() {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			visit1[i][j] = 0;
		}
	}
}

// lan0
void dfs1(int x, int y) {
	//cout << x << " " << y << endl;
	zero[++cur0].x = x;
	zero[cur0].y = y;
	visit0[x][y] = 1;
	
	for (int i = 0; i < 4; i++) {
		int x_next = x + dx[i];
		int y_next = y + dy[i];
		if (inside(x_next, y_next) && visit0[x_next][y_next] == 0) {
			if (a[x_next][y_next] == 0) {
				//cout << x_next << " " << y_next << endl;
				dfs1(x_next, y_next);
			}
			else {
				//cout << x_next << " " << y_next << endl;
				visit0[x_next][y_next] = 1;
				xungquanh[++cur].x = x_next;
				xungquanh[cur].y = y_next;
				//cout << cur << endl;
				//cout << xungquanh[cur].x << " " << xungquanh[cur].y << endl;
			}
		}
	}
	
}

void dfs2(int x, int y, int vung) {
	
	visit1[x][y] = 1;
	tanso[vung] += 1;
	
	for (int i = 0; i < 4; i++) {
		int x_next = x + dx[i];
		int y_next = y + dy[i];
		
		if (inside(x_next, y_next) && visit1[x_next][y_next] == 0) {
			if (a[x_next][y_next] == vung) {
				dfs2(x_next, y_next, vung);
			}
		}
		
	}
	
}

void solve(int testcase) {
	
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
		}
	}
	int ok = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			
			if (a[i][j] == 0 && visit0[i][j] == 0) {
				cur0 = 0;
				cur = 0;
				dfs1(i, j);
				
				max_tanso = -1;
				max_vung = -1;
				resetvisit1();
				
				for (int i = 1; i <= cur; i++) {
					cout << xungquanh[i].x << " " << xungquanh[i].y << " " << a[xungquanh[i].x][xungquanh[i].y] << endl;
					//dfs2(xungquanh[i].x, xungquanh[i].y, a[xungquanh[i].x][xungquanh[i].y]);
				}
				break;
			}
			
			
		}
	}
	/*
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cout << visit0[i][j] << " ";
		}
		cout << endl;
	}
	*/
	
	
}

int main() {
	freopen("Text.txt", "r", stdin);
	int t; cin >> t;
	
	for (int i = 1; i <= t; i++) {
		solve(i);
	}
	
}
Leave a Comment