Counting Islands II_太阁竞赛C
来源:互联网 发布:淘宝男士护肤 编辑:程序博客网 时间:2024/04/28 03:28
题目3 : Counting Islands II
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
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
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
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;}
0 0
- Counting Islands II_太阁竞赛C
- Binary Watch_太阁竞赛A
- Popular Products_太阁竞赛B
- [随便切水题系列]hihoCoder太阁算法竞赛12
- hihoCoder太阁最新面经算法竞赛题解(1)
- hihoCoder太阁最新面经算法竞赛题解(2)
- hihoCoder太阁最新面经算法竞赛题解(3)
- hihoCoder太阁最新面经算法竞赛题解(4)
- hihoCoder太阁最新面经算法竞赛题解(5)
- hihoCoder太阁最新面经算法竞赛题解(6)
- hihoCoder太阁最新面经算法竞赛题解(7)
- Islands
- islands
- 《hihoCoder太阁最新面经算法竞赛4》题目解析【含源代码】
- [hihoCoder 太阁最新面经算法竞赛6] 数组重排(N个数的最小公倍数)
- [hihoCoder太阁最新面经算法竞赛2] 岛屿 (DFS,Hash Function)
- hihoCoder太阁最新面经算法竞赛8 A Game(区间DP)
- hihoCoder太阁最新面经算法竞赛8 题目2 : Dice Possibility (概率DP)
- JComboBox默认不选中任何选项的方法
- 2N的数组分割成两个和最接近的子数组
- 如何在c++中调用java代码
- Gulp-前端进阶A-2
- RxJava新手入门之二 快捷发送事件just方法使用以及from拆分数组发送事件方法详解
- Counting Islands II_太阁竞赛C
- 生活需要仪式感
- c++知识
- 乘法逆元的几种求法
- 坚韧是种态度
- 多线程编程-- 线程安全的queue II
- git 将一个本地文件目录提交到远程仓库的步骤
- 有序集合求交集
- Give My Text Back_hihoCoder