Uncle Tom's Inherited Land*
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3339 Accepted Submission(s): 1394
Special Judge
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).
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.
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.
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
(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)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <queue>
#include <vector>
#define inf 0x7fffffff
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int read() {int x=,f=;char c=getchar();while(c<''||c>'') {if(c=='-')f=-;c=getchar();}while(c>=''&&c<='') {x=x*+c-'';c=getchar();}return x*f;}
int n,m,cnt;
int dis[][]={{,},{,-},{,},{-,}}; int tot,head[N];
int w[][],t[N],x[N],y[N];
struct man
{
int x,y,color,num;
};
struct EDG
{
int to,next;
}edg[M];
struct ANS
{
int x,y;
}answ[N];
void add(int u,int v)
{
edg[cnt].to=v;edg[cnt].next=head[u];head[u]=cnt++;
}
void bfs(int u,int v)
{
queue<man>q;
man s;s.color=;s.num=++tot;s.x=u;s.y=v;q.push(s);
w[u][v]=;answ[tot].x=u;answ[tot].y=v;
while(!q.empty()){
man t=q.front();q.pop();
for(int i=;i<;i++){
int xx=t.x+dis[i][];int yy=t.y+dis[i][];
if(xx>=&&xx<=n&&yy>=&&yy<=m&&!w[xx][yy]){
man k;k.color=(t.color+)%;k.num=++tot;k.x=xx;k.y=yy;
q.push(k);w[xx][yy]=;answ[k.num].x=xx;answ[k.num].y=yy;
if(!t.color)add(t.num,k.num);
else add(k.num,t.num);
}
}
}
}
bool dfs(int u) {
for(int i=head[u];i!=-;i=edg[i].next) {
int v=edg[i].to;
if(!t[v]) {
t[v]=;
if(!y[v]||dfs(y[v])) {
x[u]=v;
y[v]=u;
return true;
}
}
}
return false;
}
void MaxMatch() {
int ans=;
for(int i=; i<=tot; i++) {
if(!x[i]) {
met(t,);
if(dfs(i))ans++;
}
}
printf("%d\n",ans);
for(int i=;i<=tot;i++){
if(x[i]){
int v=x[i];
printf("(%d,%d)--(%d,%d)\n",answ[i].x,answ[i].y,answ[v].x,answ[v].y);
}
}
printf("\n");
}
int main() {
while (~scanf("%d%d",&n,&m)&&n&&m) {
met(w,);met(head,-);met(x,);met(y,);met(edg,);met(answ,);cnt=;tot=;
int k=read();
while(k--){
int x=read();int y=read();
w[x][y]=;
}
for(int i=;i<=n;i++)for(int j=;j<=m;j++)if(!w[i][j])bfs(i,j);
MaxMatch();
}
return ;
}