Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.9 kB
2
Indexable
Never
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

 #include<iostream>
using namespace std;
int mang[30][30], visit[30][30], N, M;
int qx[30*30], qy[30*30], front, rear;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int tong;
void sky(int a, int b)
{
    rear = front = -1;
    rear++;
    qx[rear] = a;
    qy[rear] = b;
    visit[a][b] = 1;
    while(rear != front)
    {
        front++;
        int a1 = qx[front];
        int b1 = qy[front];
        for(int i1 = 0; i1< 4; i1++)
        {
            int a2 = a1 + dx[i1];
            int b2 = b1 + dy[i1];
            if(a2 >= 0 && a2 < N && b2 >=0 && b2 < N && visit[a2][b2] == 0 && mang[a2][b2] == 1)
            {
                visit[a2][b2] = 1;
                tong = tong + 1;
                rear++;
                qx[rear] = a2;
                qy[rear] = b2;
            }
        }
    }
}
int main()
{
//  freopen("Text.txt","r",stdin);
    int stc;
    cin>>stc;
    for(int tc = 1; tc <= stc; tc++)
    {
        cin>>N;
        for(int i = 0; i< N; i++)
        {
            for(int j = 0; j< N; j++)
            {
                cin>>mang[i][j];
                visit[i][j] = 0;
            }
        }
        int dem  = 0;
        int somax = 0;
        for(int i = 0; i< N; i++)
        {
            for(int j = 0; j< N; j++)
            {
                if(visit[i][j] == 0 && mang[i][j] == 1)
                {
                    dem = dem + 1;
                    tong = 1;
                    sky(i,j);
                    if(tong > somax)
                {
                    somax = tong;
                }
                }
            }
        }
        cout<<dem<<" "<<somax<<endl;
    }
    return 0;
}