Counting Islands II

描述

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>
#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cstring>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
const int maxn=1e6+;
using namespace std;
int n,m,p[maxn],ans;
char mip[][];
int fa(int x)
{
return p[x]==x?x:p[x]=fa(p[x]);
}
void work(int x,int y)
{
ans++;
int a,b;
if(x->=&&mip[x-][y]=='#')
{
a=fa(x*+y),b=fa((x-)*+y);
if(a!=b)p[a]=b,ans--;
}
if(x+<&&mip[x+][y]=='#')
{
a=fa(x*+y),b=fa((x+)*+y);
if(a!=b)p[a]=b,ans--;
}
if(y->=&&mip[x][y-]=='#')
{
a=fa(x*+y),b=fa(x*+y-);
if(a!=b)p[a]=b,ans--;
}
if(y+<&&mip[x][y+]=='#')
{
a=fa(x*+y),b=fa(x*+y+);
if(a!=b)p[a]=b,ans--;
}
return;
}
int main()
{
int i,j,k,t;
rep(i,,maxn-)p[i]=i;
memset(mip,'.',sizeof(mip));
scanf("%d",&n);
while(n--)
{
int x,y;
scanf("%d%d",&x,&y);
mip[x][y]='#';
work(x,y);
printf("%d\n",ans);
}
//system("pause");
return ;
}

Counting Islands II的更多相关文章

  1. hihocoder Counting Islands II(并查集)

    Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...

  2. [LeetCode] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  3. [LeetCode] 305. Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  4. [LeetCode] Number of Islands II

    Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may pe ...

  5. Leetcode: Number of Islands II && Summary of Union Find

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  6. 305. Number of Islands II

    题目: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand  ...

  7. [LeetCode] Number of Distinct Islands II 不同岛屿的个数之二

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  8. [Swift]LeetCode305. 岛屿的个数 II $ Number of Islands II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  9. LeetCode – Number of Islands II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

随机推荐

  1. fragement切换动画效果的实现

    标准动画: fragementTransaction.setTransition(FragmentTransation.TRANSIT_FRAGMENT_CLOSE); 自定义动画: fragemen ...

  2. 使用CodeFirst实现动态建库

    一.业务分析 以我们平时注册今目标为例,我们在注册今目标的过程中,具体步骤是这样的: 图1 今目标登陆流程 详细解释一下: 第一步:注册界面.输入手机号或者邮箱,点击确定进入基本信息界面. 第二步:基 ...

  3. Cantor数表

    题目:现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 第一项是1/1,第二项是是1/2,第三项是2/1,第四项是3/1,第五项是2/2,… ...

  4. android,view的执行过程onDraw、onSizeChanged,onFinishInflate

    小试view的执行过程,此是入门,高手绕道. ----------------------------------------------------------------------------- ...

  5. linkButton

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  6. C++11 半同步半异步线程池的实现

    #include <list> #include <mutex> #include <thread> #include <condition_variable ...

  7. call_grant_dml.sql

    set echo offpromptprompt =========================================================================== ...

  8. String.Format(string, arg0)中sring格式

    复合格式字符串和对象列表将用作支持复合格式设置功能的方法的参数.复合格式字符串由零个或多个固定文本段与一个或多个格式项混和组成.固定文本是所选择的任何字符串,并且每个格式项对应于列表中的一个对象或装箱 ...

  9. OpenGL------版本历史

    到今天为止,正式的OpenGL已经有九个版本.(1.0, 1.1, 1.2, 1.2.1, 1.3, 1.4, 1.5, 2.0, 2.1)每个OpenGL版本的推出,都增加了一些当时流行的或者迫切需 ...

  10. Hibernate 系列教程1-枚举单例类

    你还在为不知道怎样正确使用Hibernate而纠结吗 你还在为不知道怎样配置映射文件而郁闷吗 枚举单例(Enum Singleton) 是实现单例模式的一种方式而已,不过写法简单,创建枚举默认也是线程 ...