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. 【第二篇】学习 android 事件总线androidEventbus之异步事件的传递

    1,不同Activity直接发送Ansy的事件,以及其他任何事件,必须通过 postSticky方式来进行事件的传递,而不能通过post的形式来进行传递:EventBus.getDefault().p ...

  2. 三个JS函数闭包(closure)例子

    闭包是JS较难分辨的一个概念,我只是按自己的理解写下来,如有不对还请指出. 函数闭包是指当一个函数被定义在另一个函数内部时,这个内部函数使用到的变量会被封闭起来形成一个闭包,这些变量会保持形成闭包时设 ...

  3. leetcode415---字符串大数相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  4. leetcode136 利用异或运算找不同的元素

    Given an array of integers, every element appears twice except for one. Find that single one. Note: ...

  5. git基本命令--远程

    git clone: # clone到 <本地目录名> $ git clone <版本库的网址> <本地目录名> # 克隆版本库的时候,所使用的远程主机自动被Git ...

  6. VituralBox 虚拟机网路设置 主机无线

    主机和虚拟机都关闭防火墙 主机和虚拟机可以互相ping通 主机和虚拟机都可以联网 如果出现  ‘device not managed by NetworkManager’ 错误 network服务器 ...

  7. adb 卸载android系统程序

    下面是通过 pm list packages -f 列出手机中的软件,然后跟模拟器中的软件进行对比后得出的可以安全卸载的列表.   注意:卸载之后就没有Google Market了,还想用google ...

  8. rm: 无法删除 "xxxxx.o" : 输入/输出错误.

    rm: 无法删除 "xxxxx.o" : 输入/输出错误. 碰到无法删除的文件,以为完蛋了,要重装. 后面重启一下就可以了

  9. SQL SERVER中强制类型转换cast和convert的区别

    在SQL SERVER中,cast和convert函数都可用于类型转换,其功能是相同的, 只是语法不同. cast一般更容易使用,convert的优点是可以格式化日期和数值. 代码 select CO ...

  10. 【floyd 多源最短路】 poj 1125

    #include <stdio.h> #include <iostream> #include <memory.h> using namespace std; ][ ...