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 ...
随机推荐
- Elasticsearch Java虚拟机配置详解(转)
引言: 今天,事情终于发生了.Java6(Mustang),是2006年早些时候出来的,至今仍然应用在众多生产环境中,现在终于走到了尽头.已经没有什么理由阻止迁移到Java7(Dolphin)上了. ...
- oracle 存储过程(1)
--建一张user_info表create table user_info (id varchar2(10),name varchar2(20),password varchar2(20),addre ...
- mysql分页的问题
用过mysql的人肯定知道,mysql提供了原生的分页功能-----LIMIT关键字.LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数.LIMIT 接受一个或两个数字参数.参数必须是 ...
- PHPExcel解决内存占用过大问题-dw 查找memoryCacheSize把1M改为2048M
http://blog.sina.com.cn/s/blog_4ec7952d0101fcrd.html PHPExcel解决内存占用过大问题-设置单元格对象缓存 PHPExcel是一个很强大的处理E ...
- UI篇—UITableview
一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UIScrollView,因此支 ...
- httpclient调用方法
/** * GET请求 * * @param url * 请求url,参数拼在请求串中 */ public static String get(String url) ...
- 实现jsp页面显示用户登录信息,利用session保存。
这是后台代码 这是jsp代码,上面是声明,下面是获得值.
- 获取表空间的语句 以及 建表和索引的ddl
alter session set container=PHD1; SET SERVEROUTPUT ON SET LINESIZE SET FEEDBACK OFF SET PAGESIZE sel ...
- 转载 Deep learning:六(regularized logistic回归练习)
前言: 在上一讲Deep learning:五(regularized线性回归练习)中已经介绍了regularization项在线性回归问题中的应用,这节主要是练习regularization项在lo ...
- makefile的编写规则
2. 编写makefile 示例: test:main.o func.o gcc -o test main.o func.o func.o:func.c gcc -c func.c mai ...