Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

400题留念~我也贴个代码,排序+floodfill

Posted by Rieman at 2010-08-05 23:30:20 on Problem 1838
#include <iostream>
using namespace std;
#define inf 16001
typedef struct tre
{
  int x,y,left,right,top,down,used,rank;
}tre;
tre tree[inf],sort[inf];
int zone[inf],zk;
void flood(int n)
{
  if(!tree[n].used&&n)
  {
	zone[zk]++;
	tree[n].used=1;
	flood(tree[n].left);
	flood(tree[n].right);
	flood(tree[n].top);
	flood(tree[n].down);
  }
}
int cmpx(const void *a,const void *b)
{
  tre *c=(tre*)a;
  tre *d=(tre*)b;
  if(c->x!=d->x)
	return c->x - d->x;
  else
	return c->y - d->y;
}
int cmpy(const void *a,const void *b)
{
  tre *c=(tre*)a;
  tre *d=(tre*)b;
  if(c->y!=d->y)
	return c->y - d->y;
  else
	return c->x - d->x;
}
int cmp(const void *a,const void *b)
{
  int *c=(int*)a;
  int *d=(int*)b;
  return *d-*c;
}
int main()
{ 
  int n,dk,i,dx,dy,sum;
  cin>>n>>dk;
  for(i=1;i<n+1;i++)
  {
	cin>>dx>>dy;
	tree[i].x=sort[i].x=dx;
	tree[i].rank=sort[i].rank=i;
	tree[i].y=sort[i].y=dy;
  }
  qsort(sort+1,n,sizeof(sort[0]),cmpx);
  dx=1;
  while(dx<n+1)
  {
	while(sort[dx].x==sort[dx+1].x)
	{
	  if(sort[dx+1].y-sort[dx].y==1)
	  {
		tree[sort[dx].rank].right=sort[dx+1].rank;
		tree[sort[dx+1].rank].left=sort[dx].rank;
	  }
	  dx++;
	}
	dx++;
  }
  qsort(sort+1,n,sizeof(sort[0]),cmpy);
  dx=1;
  while(dx<n+1)
  {
	while(sort[dx].y==sort[dx+1].y)
	{
	  if(sort[dx+1].x-sort[dx].x==1)
	  {
		tree[sort[dx].rank].down=sort[dx+1].rank;
		tree[sort[dx+1].rank].top=sort[dx].rank;
	  }
	  dx++;
	}
	dx++;
  }
  zk=1;
  for(i=1;i<n+1;i++)
	if(tree[i].used==0)
	{
	  flood(i);
	  zk++;
	}
  qsort(zone+1,zk-1,4,cmp);
  sum=0;
  for(i=1;i<dk+1;i++)
	sum=sum+zone[i];
  cout<<sum<<endl;
  return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator