Counting Islands II
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的更多相关文章
- hihocoder Counting Islands II(并查集)
Counting Islands II 描述 Country H is going to carry out a huge artificial islands project. The projec ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
随机推荐
- 安装、配置、启动FTP、SSH或NFS服务
(1)准备使用软件维护工具apt-get. Ubuntu7.10中没有安装FTP.SSH.NFS服务器软件,它提供了一个很方便的安装.升级.维护软件的工具apt-get.apt-get从光盘.网络上下 ...
- 【第一篇】Python基础
Python学习 学习站点:https://www.shiyanlou.com/ 1 hello world code如下: $ python [15:50:40] Python2.7.6(defau ...
- 【转】解决Windows不能在本地计算机启动apache tomcat
http://blog.163.com/ftskwsg@126/blog/static/5623853020094494117827/ 这个方法解决了我的问题. 在windows下以服务的方式启动时提 ...
- 算法入门(C++)
iostream,这个头文件里有很多常用的函数,比如swap交换两个变量的值,max求两个值的最大值等. cstdio头文件,这个头文件里包含C风格的输入输出.如果你之前学习过C++语言应该知道cin ...
- 在选定的数据源上未找到名为“TitleSub”的字段或属
在.NET开发过程中时常会遇到“在选定的数据源上未找到名为“TitleSub”的字段或属性”的错误”,导致这类错误的原因有很多,在我的项目中,详细情况是这样:1.有两个控件:DropDownList类 ...
- apt-get 总结
转自: apt-get 总结 1.apt-get install <package_name> install a new package. 2.apt-get build-dep < ...
- parseSdkContent failed 解决方案
开Eclipse出现错误“parseSdkContent failed”,Android的模拟器启动不了.尝试了不少方法,终于搞定. 1.删除文件夹 C:\Documents and Settings ...
- Mac 下格式化U盘
diskutil list 查看U盘盘符: lapommedeMacBook-Pro:~ lapomme$ diskutil list /dev/disk0 (internal, physical): ...
- c++ lower_bound upper_bound
lower_bound, first greater than or equal to upper_bound, first strickly greater
- unknown filesystem type ‘iso9660’类型问题--Ubuntu
unknown filesystem type ‘iso9660’是指系统不支持这种类型的文件, 用以下命令更新内核即可: sudo aptitude update sudo aptitude upg ...