Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.6 kB
2
Indexable
#include <iostream>
#define N 701
using namespace std;
int n,m;
int a[N][N];
int sx[N*N];
int sy[N*N];
int cnt,ans;
int dd[N][N];
int hx[]={-1,-1,-1,0,1,1,1,0};
int hy[]={-1,0,1,1,1,0,-1,-1};
int h[10004][N];
int b[N*N];
int doiso(int x,int y)
{
	return (x-1)*m+y;
}
void doi(int x,int &u,int &v)
{
	u=(x-1)/m+1;
	v=(x-1)%m+1;
}
void doc()
{
	cnt=0;
	ans=0;
	for(int i=0;i<=10000;i++)h[i][0]=0;
	cin>>n>>m;
	//cout<<n<<" "<<m<<"\n";
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
			{
				cin>>a[i][j];
				int u=a[i][j];
				dd[i][j]=0;
				h[u][0]++;
				h[u][h[u][0]]= doiso(i,j);
				//cout<<h[u][h[u][0]]<<" ";
		}
		//cout<<"\n";
	}
		int id=0;
		for(int i=10000;i>=0;i--)
		{
			//if(h[i][0])cout<<i<<" - "<<h[i][0]<<"\n";
			for(int j=1;j<=h[i][0];j++)
				{
					b[++id]=h[i][j];
					//cout<<b[id]<<"\n";
			}
		}
}
void bfs(int x,int y)
{
	//cout<<x<<" - "<<y<<"\n";
	cnt++;
	
	int dau=1,cuoi=0;
	sx[++cuoi]=x;
	sy[cuoi]=y;
	dd[x][y]=1;
	while(dau<=cuoi)
	{
		int u=sx[dau];
		int v=sy[dau];
		dau++;
		for(int i=0;i<8;i++)
		{
			int xx=u+hx[i];
			int yy=v+hy[i];
			if(xx>0&&yy>0&&xx<=n&&yy<=m&&dd[xx][yy]==0&&a[xx][yy]<=a[u][v])
			{
				
				dd[xx][yy]=1;
				sx[++cuoi]=xx;
				sy[cuoi]=yy;
				
			}
		}
	}
	
}
int main()
{
	//freopen("input.txt","r",stdin);
	int TC;
	cin>>TC;
	for(int tc=1;tc<=TC;tc++)
	{
		doc();
		for(int i=1;i<=n*m;i++)
		{
			int x,u,v;
			x=b[i];
			doi(x,u,v);
			if(dd[u][v]==0)bfs(u,v);
		}
		cout<<"#"<<tc<<" "<<cnt<<"\n";
	}
	return 0;
}