题意:有一个N*M的建筑物,第一个建筑物的高度可能不等。现在给出T天,
每一天的水都上涨,第i天的水位为T[i],
求每天水把这些建筑物分成几块。
思路:这个二维并查集的名字,其实是自己取的。
思想是并查集的思想,只是用到二维上面。所以要用一个结构体来实现。
做法是从上到下。从水位最高的一个一个的建筑的加入,根据它与它周边的建筑高度的关系进行合并。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int T[100050];
int t;
int map[1001][1001];
struct my
{
int x,y;
int value;
friend bool operator<(my a,my b)
{
return a.value>b.value;
}
}go[1000100];
struct car
{
int x,y;
bool operator==(car b)
{
return x==b.x&&y==b.y;
}
}f[1001][1001];
int ngo;
bool cmp(my a,my b)
{
return a.value<b.value;
}
int n,m;
int a[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
bool in(int x,int y)
{
return x>=1 && x<=n && y>=1 && y<=m;
}
car find(int x,int y)
{
if (x==f[x][y].x && y==f[x][y].y)
return f[x][y];
return f[x][y]=find(f[x][y].x,f[x][y].y);
}
bool b[1001][1001];
int main()
{
freopen("in.txt","r",stdin);
int i,j,k;
int z;
cin>>z;
while (z--)
{
scanf("%d%d",&n,&m);
ngo=0;
for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
{
scanf("%d",&map[i][j]);
go[ngo].x=i;
go[ngo].y=j;
go[ngo++].value=map[i][j];
f[i][j].x=i;
f[i][j].y=j;
b[i][j]=false;
}
}
sort(go,go+ngo);
cin>>t;
for (i=1;i<=t;i++)
scanf("%d",T+i);
int ans=0,cur;
j=0;
T[t+1]=0;
car cc[5];
/*
for (i=0;i<ngo;i++)
cout<<i<<' '<<go[i].x<<' '<<go[i].y<<' '<<go[i].value<<endl;
*/
for (i=t;i;i--)
{
// cout<<i<<' '<<j<<endl;
int temp=T[i+1];
while (j<ngo && go[j].value>T[i])
{
cur=0;
b[go[j].x][go[j].y]=true;
//cout<<endl;
for (k=0;k<4;k++)
{
int x=a[k][0]+go[j].x;
int y=a[k][1]+go[j].y;
// cout<<" "<<x<<' '<<y<<' '<<map[x][y]<<' '<<b[x][y]<<endl;
if (in(x,y) && map[x][y]>T[t] && b[x][y])
{
cc[cur++]=find(x,y);
}
}
ans=cur;
for (int ii=0;ii<cur;ii++)
for (int jj=ii+1;jj<cur;jj++)
if (cc[ii]==cc[jj])
{
ans--;
break;
}
for (k=0;k<cur;k++)
{
f[cc[k].x][cc[k].y].x=go[j].x;
f[cc[k].x][cc[k].y].y=go[j].y;
}
temp+=1-ans;
//cout<<ans<<' '<<cur<<' '<<go[j].x<<' '<<go[j].y<<endl;
j++;
}
T[i]=temp;
}
for (i=1;i<=t;i++)
{
printf("%d ",T[i]);
}
printf("\n");
}
return 0;
}
Islands
Time Limit: 15000 ms Memory Limit: 65535 kB Solved:40 Tried: 187
Description
Deep in the Carribean, there is an island even stranger than the Monkey Island, dwelled by Horatio Torquemada Marley. Not only it has a rectangular shape, but is also divided into an n x m grid. Each grid field has a certain height. Unfortunately, the sea level started to raise and in year i, the level is i meters. Another strange feature of the island is that it is made of sponge, and the water can freely flow through it. Thus, a grid field whose height is at most the current sea level is considered flooded.Adjacent unflooded fields (i.e., sharing common edge) create unflooded areas. Sailors are interested in the number of unflooded areas in a given year.
An example of a 4 x 5 island is given below. Numbers denote the heights of respective fields in meters.Unflooded fields are darker; there are two unflooded areas in the first year and three areas in the second year.
Input
Multiple Test Cases
The input contains several test cases. The first line of the input contains a positive integer Z <= 20,denoting the number of test cases. Then Z test cases follow, each conforming to the format described in section Single Instance Input. For each test case, your program has to write an output conforming to the format described in section Single Instance Output.
Single Instance Input
The first line contains two numbers n and m separated by a single space, the dimensions of the island, where 1 <= n,m <= 1000. Next n lines contain m integers from the range [1, 10^9] separated by single spaces, denoting the heights of the respective fields. Next line contains an integer T (1 <= T <= 10^5). The last line contains T integers tj , separated by single spaces, such that 0 <= t1 <= t2 <= ... <= tT <= 10^9
Output
Single Instance Output
Your program should output a single line consisting of T numbers rj separated by single spaces, where rj is the number of un
flooded areas in year tj .
Sample Input
1
4 5
1 2 3 3 1
1 3 2 2 1
2 1 3 4 3
1 2 2 2 2
5
1 2 3 4 5
Sample Output
2 3 1 0 0