Skymap

 avatar
quoc14
c_cpp
5 months ago
3.4 kB
1
Indexable
DFS and BFS
Level 3
Sky map
You are making “sky map” to represent constellation (group of starts) in the sky.

In each element, 1 means star, 0 means empty.

And in a constellation, each start should have at least one connection with others via left/right/up/bottom. 

If two starts are located via diagonal, then

the two stars are not connected (belong to different constellation.)

How many constellations are in your sky map, and what is the number of starts in the greatest constellation?

[Input]

There can be more than one test case in the input.

The first line has T, the number of test cases.

Then the totally T test cases are provided in the following lines (T<=10)

In each test case, the first line has an integer N(5 ≤ N ≤ 25), the size of map.

The map is a square, and is represented as N x N matrix.

For next N lines, each contains each raw of the matrix

[Output]

For each test case, you should print the number of constellation and the number of starts in the greatest constellation separated by blank.

[I/O Example]

Input

2
7
0 1 1 0 0 0 0
0 1 1 0 1 0 0
1 1 1 0 1 0 1
0 0 0 0 1 1 1
1 0 0 0 0 0 0
0 1 1 1 1 1 0
0 1 0 1 1 0 0
5
0 1 0 0 0
0 1 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 0

Output

4 8
1 3

3 6
4 21
7 36
4 8
1 3

//input
5
5
1 0 0 0 0
0 1 1 1 0
1 1 0 0 0
1 0 1 1 1
0 0 1 0 1
9
0 0 1 1 1 1 1 1 1
0 0 1 0 0 0 0 1 1
1 0 1 0 1 0 0 0 1
1 1 1 1 1 0 1 0 1
0 0 1 0 0 1 1 0 0
0 0 0 0 1 1 0 0 0
0 1 1 0 0 0 0 1 0
1 0 1 0 0 1 0 1 0
1 1 1 0 0 1 1 1 1
17
1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0 1 1 0 0 0 0 0
0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0
0 0 1 1 0 0 0 1 1 1 1 0 0 0 1 1 0
0 1 1 0 0 0 0 1 0 0 1 0 0 0 1 1 0
1 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 1
0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1
0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0
0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0
0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0
0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 1 1 0 0 1 1 0 0 1 1 0
0 1 0 0 0 0 0 1 1 1 1 0 0 0 1 1 0
0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0
7
0 1 1 0 0 0 0
0 1 1 0 1 0 0
1 1 1 0 1 0 1
0 0 0 0 1 1 1
1 0 0 0 0 0 0
0 1 1 1 1 1 0
0 1 0 1 1 0 0
5
0 1 0 0 0
0 1 0 0 0
0 1 0 0 0
0 0 0 0 0
0 0 0 0 0

#include <iostream>
using namespace std;

int T, n, maxStar, cntCons, c;
int mp[26][26];
bool visit[26][26];

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

void dfs(int i, int j){
	if(visit[i][j]) return;
	visit[i][j] = true;
	if(c > maxStar) maxStar = c;
	for(int k = 0; k < 4; k++){
		int x = i + dx[k];
		int y = j + dy[k];
		if(x >= 0 && x < n && y >= 0 && y < n && mp[x][y] == 1 && !visit[x][y]){
			c++;
			dfs(x, y);
		}
	}
}



int main(){
	freopen("input.txt", "r", stdin);
	cin >> T;
	
	for(int tc = 1; tc <= T; tc++){
		// Input
		cin >> n;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				cin >> mp[i][j];
				visit[i][j] = false;
			}
		}
		
		// Initial
		maxStar = 0, cntCons = 0;
		
		// Solve Problem
		for(int i = 0; i < n;  i++){
			for(int j = 0; j < n; j++){
				if(mp[i][j] && !visit[i][j]){
					c = 1;
					dfs(i, j);
					cntCons++;
				}
			}
		}
		
		// Output
		cout << cntCons << " " << maxStar << endl;
	}

	return 0;
 }
Leave a Comment