Counting Islands II_太阁竞赛C

版权声明:本文为博主原创文章,未经博主允许不得转载,谢谢合作! https://blog.csdn.net/cleopard66/article/details/52081818
题目3 : Counting Islands II
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Country H is going to carry out a huge artificial islands project. The project region is divided into a 1000x1000 grid. The whole project will last for N weeks. Each week one unit area of sea will be filled with land.


As a result, new islands (an island consists of all connected land in 4 -- up, down, left and right -- directions) emerges in this region. Suppose the coordinates of the filled units are (0, 0), (1, 1), (1, 0). Then after the first week there is one island:  


#...
....
....
....
After the second week there are two islands:  


#...
.#..
....
....
After the three week the two previous islands are connected by the newly filled land and thus merge into one bigger island:


#...
##..
....
....
Your task is track the number of islands after each week's land filling.  


输入
The first line contains an integer N denoting the number of weeks. (1 ≤ N ≤ 100000)  


Each of the following N lines contains two integer x and y denoting the coordinates of the filled area.  (0 ≤ x, y < 1000)


输出
For each week output the number of islands after that week's land filling.


样例输入
3  
0 0   
1 1   
1 0   
样例输出
1  
2  

1  


【我的程序】

#include <iostream>
using namespace std;

long int n, num=0, a[1000000]={0}, aSize[1000000]={0};
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

long int findID(long int x)
{
    while (a[x]!=x) x=a[x]=a[a[x]];
    return x;
}

void unionID(long int x, long int y)
{
    x=findID(x); y=findID(y);
    if (x==y) return;
    if (aSize[x]<aSize[y]) {a[x]=y; aSize[y]+=aSize[x];}
    else {a[y]=x; aSize[x]+=aSize[y];}
    num--;
}

int check(int x, int y)
{
    if (x>=0 && x<1000 && y>=0 && y<1000 && aSize[x*1000+y]>0) return 1; else return 0;
}

int main()
{
    long int i,p,q;
    cin>>n;

    for (i=0;i<n;i++)
    {
        cin>>p >>q;
        a[p*1000+q]=p*1000+q;
        aSize[p*1000+q]=1;
        num++;

        for (int j=0;j<4;j++)
            if (check(p+dx[j],q+dy[j])) unionID(p*1000+q,(p+dx[j])*1000+q+dy[j]);

        cout<<num <<endl;
    }

    return 0;
}


Islands

04-10

You are working in a team developing the new strategic game Island Warriors. The game proceeds on a hexagonal grid, the coordinate system on the map is introduced as shown on the picture below.nnnA hexagon can be either land, or sea. An island is a maximal connected set of land hexagons, an area of the island is the number of hexagons in it.nnYour current task is writing random map generator. The map generated must satisfy some conditions, in particular no island area must exceed s.nnThe design of your module is the following. Initially all hexagons are sea. Special random generator provides you with the sequence of hexagons. Getting the next hexagon, you check whether it is already the land one. If it is, you just skip this hexagon. If it is sea, you check whether convering it to land results in an island with area exceeding s. If it does, you also skip this hexagon. In the other case you convert it to land.nnSo, the design step is completed, now its time to implement the module. Fortunately, your teammate has already implemented random generator, so you just have to implement the rest of the module. To check yourself you decided first to output only the number of islands in the resulting map and their areas.nnInputnnThe first line of the input file contains n --- the number of hexagons provided to you by random generator (1 ≤ n ≤ 50000), and s (1 ≤ s ≤ n).nnThe following n lines contain two integer numbers each --- coordinates of the hexagons. Coordinates do not exceed 500 by their absolute values.nnThere are multiple cases. Process to the end of file.nnOutputnnOutput the number of islands in the resulting map and their areas. List areas in the ascending order.nnSample Inputnn7 4n0 1n2 1n3 0n1 -1n2 1n1 0n0 0nSample Outputnn2n2 3 问答

没有更多推荐了,返回首页