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

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)

 【分析】给出一个矩形图 将其中一些格子涂黑 问连续的两个白格子最多多少对。可以将每一个白色格子看成一  个点,然后分成两类 即每个点与他相邻的点分为两类,然后连边 二分图匹配
#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 ;
}

最新文章

  1. jQuery实用工具函数
  2. Elasticsearch 教程--入门
  3. 运用datalist标签实现用户的搜索列表
  4. 说说分析bug的一些心得
  5. 转 CSS hack:针对IE6,IE7,firefox显示不同效果
  6. VFL +AutoLayout
  7. 搭建eclipse环境下 Nutch+Mysql 二次开发环境
  8. Git CMD - fetch: Download objects and refs from another repository
  9. PHP程序效率优化
  10. Java代码之输出参数和(强制类型转换)
  11. How to make a simplest WCF service work on Win7 with VS2010
  12. jquery 导出Excel表格
  13. Java中的List转换成JSON报错(三)
  14. Python动态语言的特性
  15. 使用laraval框架和前端完成restful风格的请求对接(这里只是讨论restful的概念)
  16. 介绍一下Spring Cloud简介
  17. 真正的Maven经常使用命令
  18. RefreshListView下拉刷新
  19. Duboo 与springboot整合
  20. 201621123018《Java程序设计》第7周学习报告

热门文章

  1. 记一个有想法却没能力实现的硬件产品——mp3校时闹钟
  2. 看项目得到info_freeCsdn-01闪屏页面
  3. SpringMvc异常
  4. (二)iOS如何把所有界面的状态栏的字体颜色都设置为白色
  5. RelativeLayout相对布局中拖放控件的办法
  6. Apache目录结构(一)
  7. poj1837 dp
  8. 设置navigation baritem方法
  9. html网页标题
  10. 几次接触Collection排序使用总结