Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3445    Accepted Submission(s): 1452
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)
 
Source
 题意:n*m的网格,有的不能用,要求相邻的两个小方格为一组,问最多有多少组。
代码:
//简单的二分匹配,相邻的且可用的网格之间建边,求玩=完二分图之后link数组存储的就是答案。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int vis[],link[],nu1[],nu2[],pond[];
int Mu,Mv,n,m,k,last;
vector<int>g[];
bool dfs(int x)
{
for(int i=;i<g[x].size();i++){
int y=g[x][i];
if(!vis[y]){
vis[y]=;
if(link[y]==-||dfs(link[y])){
link[y]=x;
return ;
}
}
}
return ;
}
int Maxcon()
{
int ans=;
memset(link,-,sizeof(link));
for(int i=;i<=n*m;i++){
if(pond[i]) continue;
memset(vis,,sizeof(vis));
if(dfs(i)) ans++;
}
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m)&&(n+m)){
int x,y;
memset(pond,,sizeof(pond));
scanf("%d",&k);
while(k--){
scanf("%d%d",&x,&y);
pond[(x-)*m+y]=;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
int id=(i-)*m+j;
g[id].clear();
if(i>&&pond[id-m]==) g[id].push_back(id-m);
if(j>&&pond[id-]==) g[id].push_back(id-);
if(i<n&&pond[id+m]==) g[id].push_back(id+m);
if(j<m&&pond[id+]==) g[id].push_back(id+);
}
}
int ans=Maxcon()/;
printf("%d\n",ans);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
int id=(i-)*m+j;
if(link[id]==-||link[link[id]]==-) continue;
printf("(%d,%d)--(%d,%d)\n",i,j,(link[id]-)/m+,link[id]%m==?m:link[id]%m);
link[id]=link[link[id]]=-;
}
}
printf("\n");
}
return ;
}

最新文章

  1. 7.7 数据注解特性--Table
  2. 1、Jsp页面
  3. Scrum Meeting 12-20151218
  4. 关于 jsp 解析特殊字符的问题
  5. nyoj163_Phone List_字典树
  6. ECshop设置301最快捷最简单的方法
  7. 打造高仿QQ的友盟反馈界面(MVP模式)
  8. jquery.util.easyui.dialog
  9. C# 手动读写app config 的源码
  10. C# word开发
  11. Android史上功能最全的日历控件
  12. SQlSERVER生成唯一编号
  13. 【跑会指南】2017年3-5月IT技术会议大合集
  14. Large-Margin Softmax Loss for Convolutional Neural Networks
  15. [再寄小读者之数学篇](2014-07-27 $H^{-1}$ 中的有界集与弱收敛极限)
  16. SQL Server中的完全连接(full join)
  17. Maven Dependency错误——下载失败问题解决方案
  18. Why do some system users have /usr/bin/false as their shell? What&#39;s the difference between /sbin/nologin and /bin/false
  19. 阅读AuTO利用深度强化学习自动优化数据中心流量工程(一)
  20. 虚析构函数? vptr? 指针偏移?多态数组? delete 基类指针 内存泄漏?崩溃?

热门文章

  1. HADOOP-输出数据实体类承载
  2. beego 笔记
  3. iOS-【UIDynamic-UIKit动力学】
  4. Swift-创建UIButton(其他UI组件雷同)
  5. iOS-开发将文本复制到剪切板
  6. 【Linux】- vi/vim
  7. java数据结构-HashMap
  8. Error:Unable to tunnel through proxy. Proxy returns &quot;HTTP/1.1 400 Bad Request&quot;
  9. 自定义JS Map 函数
  10. CDN概念基本介绍