# Counting Islands II_太阁竞赛C

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;
}

08-01 613

04-24 493

05-23 1065

10-22 3156

10-12 56

07-30 85

05-08 13

08-23 270

03-13 59

07-23 2880

05-15 2743

09-14 1172

04-14 59万+

03-13 15万+

03-01 13万+

03-04 13万+

03-08 4万+

03-08 7万+

04-25 6万+

03-10 13万+

03-12 11万+

03-13 11万+

03-14 1万+

03-18 9570

03-19 3万+

03-19 8万+

03-19 3万+

03-22 4万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-27 1万+

03-29 21万+

03-29 10万+

03-30 16万+

05-28 5897

05-28 1万+

04-02 4512

04-02 4万+

04-06 7万+

04-09 8万+

04-09 2万+

05-17 8242

04-11 3万+

04-15 6万+

04-18 4万+

04-20 4万+

04-24 3万+

04-29 6933

04-30 9871

04-30 9437

05-01 6005

05-03 1万+

05-16 5万+

05-06 1万+

05-08 4万+

05-10 3182

05-14 6298

05-14 1872

05-16 3481

05-16 1万+

05-20 899

05-18 8248

05-27 1209

05-18 3447

05-19 1万+

05-21 7005

05-21 7026

05-27 4970

05-25 5046

05-23 6446

05-28 1346

05-23 2635

05-24 8196

05-25 5697

05-25 3512

05-25 4030

05-25 919

#### 一段价值2.4万的Python代码

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客