【算法】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

分析:

寻找矩阵中所有包含1的块数,及1的最大个数

源码:

#include <iostream>

using namespace std;

int N;
int data[26][26];

int look(int vpos, int hpos)
{
	int sum = 0;
	if (data[vpos][hpos] == 1)
	{
		sum++;
		data[vpos][hpos] = 0;
		if (hpos<N)
			sum += look(vpos, hpos + 1);
		if (vpos<N)
			sum += look(vpos + 1, hpos);
		if (hpos>1)
			sum += look(vpos, hpos - 1);
		if (vpos>1)
			sum += look(vpos - 1, hpos);
	}
	return sum;
}

int main(int argc, char** argv)
{
	int test_case;
	int T;
	int temp;
	/*
	The freopen function below opens input.txt file in read only mode, and afterward,
	the program will read from input.txt file instead of standard(keyboard) input.
	To test your program, you may save input data in input.txt file,
	and use freopen function to read from the file when using cin function.
	You may remove the comment symbols(//) in the below statement and use it.
	Use #include<cstdio> or #include<stdio.h> to use the function in your program.
	But before submission, you must remove the freopen function or rewrite comment symbols(//).
	*/
	// freopen("input.txt", "r", stdin);

	cin >> T;
	for (test_case = 0; test_case < T; test_case++)
	{
		int i, j;
		int S, C;
		cin >> N;

		for (i = 1; i <= N; i++) 
		{
			for (j = 1; j <= N; j++)
			{
				cin >> data[i][j];
			}
		}

		/**********************************
		* Implement your algorithm here. *
		**********************************/

		S = 0;
		C = 0;

		for (i = 1; i <= N; i++)
		{
			for (j = 1; j <= N; j++)
			{
				temp = look(i, j);
				if (temp > 0)
					S++;
				if (temp > C)
					C = temp;
			}
		}
			
		// Print the answer to standard output(screen).
		cout << S << " " << C << endl;
	}

	return 0;//Your program should return 0 on normal termination.
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值