hdu 1507 Uncle Tom's Inherited Land*(二分图最大匹配,黑白染色)
2016-07-28 00:09
417 查看
Uncle Tom's Inherited Land*
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3214 Accepted Submission(s): 1351
Special Judge
Problem Description
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).
Input
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.
Output
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.
Sample Input
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
Sample Output
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)
题意:有一个n*m的的矩阵,有k个坑,现在让你往里面填1*2的小方块,问你最多可以填几块
然后把每一块占的两个位置输出出来
思路:跟poj那个棋盘那个题一样,用黑白染色把棋盘染色,然后每一个方块就一定是一个黑色一个白色了,我们就可以以此建立二分图,从白色去找黑色
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 550
int dir[4][2]= {-1,0,1,0,0,-1,0,1};
int ma
,d
;
int line
,vis
;
struct Node
{
int x,y;
} p
,q
;
struct Line
{
int x,y,x1,y1;
}pp
;
int can(int t,int m)
{
for(int i=1; i<m; i++)
{
if(!vis[i]&&ma[t][i]==1)
{
vis[i]=1;
if(line[i]==-1||can(line[i],m))
{
line[i]=t;
return 1;
}
}
}
return 0;
}
bool cmp(Line a,Line b)
{
if(a.x==b.x) return a.y<b.y;
return a.x<b.x;
}
int main()
{
int n,m,k;
int x,y;
while(~scanf("%d %d",&n,&m)&&(n+m))
{
scanf("%d",&k);
memset(ma,0,sizeof(ma));
memset(line,-1,sizeof(line));
memset(d,0,sizeof(d));
for(int i=1; i<=k; i++)
{
scanf("%d %d",&x,&y);
d[x][y]=-1;
}
int cn=1,cm=1;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(d[i][j]==-1) continue;
if(i&1)
{
if(j&1) p[cn].x=i,p[cn++].y=j;
else
{
d[i][j]=cm;
q[cm].x=i,q[cm++].y=j;
}
}
else
{
if(j&1)
{
d[i][j]=cm;
q[cm].x=i,q[cm++].y=j;
}
else p[cn].x=i,p[cn++].y=j;
}
}
}
for(int i=1; i<cn; i++)
{
x=p[i].x,y=p[i].y;
for(int j=0; j<4; j++)
{
int ux=x+dir[j][0],uy=y+dir[j][1];
if(ux<1||ux>n||uy<1||uy>m||d[ux][uy]==-1) continue;
int id=d[ux][uy];
ma[i][id]=1;
}
}
int ans=0;
for(int i=1; i<cn; i++)
{
memset(vis,0,sizeof(vis));
if(can(i,cm)) ans++;
}
printf("%d\n",ans);
int num=0;
for(int i=1; i<=cm; i++)
{
if(line[i]>0)
{
int x=p[line[i]].x,y=p[line[i]].y;
int x1=q[i].x,y1=q[i].y;
if(x>x1) pp[num].x=x1,pp[num].y=y1,pp[num].x1=x,pp[num++].y1=y;
else if(x<x1) pp[num].x=x,pp[num].y=y,pp[num].x1=x1,pp[num++].y1=y1;
else
{
if(y>y1) pp[num].x=x1,pp[num].y=y1,pp[num].x1=x,pp[num++].y1=y;
else pp[num].x=x,pp[num].y=y,pp[num].x1=x1,pp[num++].y1=y1;
}
}
}
sort(pp,pp+num,cmp);
for(int i=0; i<num; i++)
printf("(%d,%d)--(%d,%d)\n",pp[i].x,pp[i].y,pp[i].x1,pp[i].y1);
printf("\n");
}
return 0;
}
相关文章推荐
- hadoop streaming多用户权限问题
- Java中clone的用法
- POJ 3616 D - Milking Time(DP)
- 优先队列--C语言实现与Java例子
- Env:Winmanager插件使用
- MalformedByteSequenceException: Invalid byte 1 of 1-byte
- POJ2187-Beauty Contest-凸包
- ls
- history命令详解和man帮助
- uboot命令
- message from server: "Host '114.111.166.7' is not allowed to connect to this MySQL server"
- openstack学习笔记八 cinder基础配置
- 2016 Multi-University Training Contest 3 题解(待续)
- 校外实习报告(十三)
- 计算机扩容,磁盘接口及选型总结
- Android基础:Eclipse关联V4包源码
- Python中字符串的常见操作技巧总结
- python中的格式化输出用法总结
- python3新特性函数注释Function Annotations用法分析
- Python随机生成数据后插入到PostgreSQL