您的位置:首页 > 其它

hdu acm 1507 Uncle Tom's Inherited Land*

2016-02-05 13:32 435 查看
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2831    Accepted Submission(s): 1171
Special Judge


[align=left]Problem Description[/align]
Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncle decided to divide the land into a grid of small
squares. He turned some of the squares into ponds, for he loved to hunt ducks and wanted to attract them to his property. (You cannot be sure, for you have not been to the place, but he may have made so many ponds that the land may now consist of several disconnected
islands.)

Your uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle has been informed that, at his great-great-uncle's request, a law has been passed which establishes that property can only be sold in rectangular lots the
size of two squares of your uncle's property. Furthermore, ponds are not salable property.

Your uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks).



 

[align=left]Input[/align]
Input will include several test cases. The first line of a test case contains two integers N and M, representing, respectively, the number of rows and columns of the land (1 <= N, M <= 100). The second line will contain an integer
K indicating the number of squares that have been turned into ponds ( (N x M) - K <= 50). Each of the next K lines contains two integers X and Y describing the position of a square which was turned into a pond (1 <= X <= N and 1 <= Y <= M). The end of input
is indicated by N = M = 0.

 

[align=left]Output[/align]
For each test case in the input your program should first output one line, containing an integer p representing the maximum number of properties which can be sold. The next p lines specify each pair of squares which can be sold simultaneity.
If there are more than one solution, anyone is acceptable. there is a blank line after each test case. See sample below for clarification of the output format.

 

[align=left]Sample Input[/align]

4 4
6
1 1
1 4
2 2
4 1
4 2
4 4
4 3
4
4 2
3 2
2 2
3 1
0 0

 

[align=left]Sample Output[/align]

4
(1,2)--(1,3)
(2,1)--(3,1)
(2,3)--(3,3)
(2,4)--(3,4)

3
(1,1)--(2,1)
(1,2)--(1,3)
(2,3)--(3,3)

 
题目大意:给你一个矩形,然后输入矩形里面池塘的坐标(不能放东西的地方),问可以放的地方中,最多可以放多少块1*2的长方形方块,并输出那些方块的位置。

解题关键:按横纵坐标的和分成奇,偶两集合,然后用匈牙利算法,一开始没分奇偶,输出方块位置会错。。。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<map>
#define maxn 20000
#define MS(a,b) memset(a,b,sizeof(a));
int dir[4][2]={-1,0,1,0,0,1,0,-1};
int n,m,h,head[maxn],vis[maxn],v[maxn],f;
int link[maxn];
struct node
{
int e,next;
}edge[500005];
struct hyf
{
int x,y;
}p[500005];
void add(int s,int e)
{
edge[f].e=e;
edge[f].next=head[s];
head[s]=f++;
}
bool dfs(int u)
{
int i,v;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].e;
if(!vis[v])
{
vis[v]=1;
if(link[v]==-1||dfs(link[v]))
{
link[v]=u;
return true;
}
}
}
return false;
}
int main()
{
int i,j,a,b,ans,k;
while(~scanf("%d %d",&n,&m))
{
if(n==0&&m==0)break;
scanf("%d",&h);
MS(v,0);
for(i=0;i<h;i++)
{
scanf("%d %d",&a,&b);
v[a*m+b]=1;
}
int tx,ty;
MS(head,-1)
f=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(v[i*m+j])continue;
if((i+j)%2==0)continue;
for(k=0;k<4;k++)
{
tx=i+dir[k][0],ty=j+dir[k][1];
if(tx<1||tx>n||ty<1||ty>m)continue;
if(v[tx*m+ty])continue;
add(i*m+j,tx*m+ty);
}
}
}
MS(link,-1);
ans=0;
for(i=m+1;i<=(n+1)*m;i++)
{
MS(vis,0);
if(link[i]==-1)
if(dfs(i))
ans++;
}
printf("%d\n",ans);
j=0;
for(i=m+1;i<=(n+1)*m;i++)
if(link[i]!=-1)
{
p[j].x=i,p[j].y=link[i];
j++;
}
int c1,d1,c2,d2;
for(i=0;i<j;i++)
{
if(p[i].x%m!=0)
c1=p[i].x/m,d1=p[i].x%m;
else
c1=p[i].x/m-1,d1=m;
if(p[i].y%m!=0)
c2=p[i].y/m,d2=p[i].y%m;
else
c2=p[i].y/m-1,d2=m;
printf("(%d,%d)--(%d,%d)\n",c1,d1,c2,d2);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: