Untitled

mail@pastecode.io avatarunknown
plain_text
2 months ago
897 B
1
Indexable
Never
#include <iostream>
using namespace std;
int a[26][26], n, cnt = 0;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
void dfs(int x, int y) {
	cnt++;
	a[x][y] = 0;
	for(int i = 0; i < 4; i++) {
		int x1 = x + dx[i];
		int y1 = y + dy[i];
		if(x1 >= 0 && x1 < n && y1 >= 0 && y1 < n && a[x1][y1] == 1) {
			dfs(x1, y1);
		}
	}
}
int main() {
	freopen("input.txt", "r", stdin);
	int T = 10; 
	cin >> T;
	for(int test_case = 1; test_case <= T; test_case++) {
		cin >> n;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++) {
				cin >> a[i][j];
			}
		}
		int maxAns = 0, constellations = 0;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				if(a[i][j] == 1) {
					cnt = 0;
					constellations++;
					dfs(i, j);
					if(cnt > maxAns) maxAns = cnt;
				}
			}
		}
		cout << constellations << " " << maxAns << endl;
	}
	return 0;
}