Uncle Tom's Inherited Land*
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 2
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)
题目大意:给你一个矩形,然后输入矩形里面池塘的坐标(不能放东西的地方),问可以放的地方中,最多可以放多少块1*2的长方形方块,并输出那些方块的位置。
这道题感觉出得很好,平时经常会遇到这些问题,但是不知道怎么解决。这个问题就是二分匹配,用匈牙利算法可以搞定,问题就是如何建图,相邻连着的就可以加一条边。这样建图后,最大匹配就是最大值。坐标就是他的pre[]这样问题就解决了。
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;
const int N = 105;
int pre[N*N], map[N*N];
bool flag[N*N], vis[N*N];
int n, m, total;
int find(int cur)
{
int k;
k = cur + 1;
if(cur%m+1 < m && k < total && map[k] != -1 && !flag[k])
{
flag[k] = true;
if(pre[k] == -1 || find(pre[k]))
{
pre[k] = cur;
return 1;
}
}
k = cur + m;
if(k < total && map[k] != -1 && !flag[k])
{
flag[k] = true;
if(pre[k] == -1 || find(pre[k]))
{
pre[k] = cur;
return 1;
}
}
k = cur - 1;
if(cur%m > 0 && k >= 0 && map[k] != -1 && !flag[k])
{
flag[k] = true;
if(pre[k] == -1 || find(pre[k]))
{
pre[k] = cur;
return 1;
}
}
k = cur - m;
if(k >= 0 && map[k] != -1 && !flag[k])
{
flag[k] = true;
if(pre[k] == -1 || find(pre[k]))
{
pre[k] = cur;
return 1;
}
}
return 0;
}
int main()
{
int i, k, t, x, y, sum;
while(scanf("%d %d", &n, &m), n && m)
{
total = n * m;
memset(vis, false, sizeof(vis));
memset(map, -1, sizeof(map));
memset(pre, -1, sizeof(pre));
for(i = 0; i < total; i++)
{
map[i] = i;
}
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &x, &y);
k = (x-1)*m + (y-1);
map[k] = -1;
}
sum = 0;
for(i = 0; i < total; i++)
{
if(map[i] != -1)
{
memset(flag, false, sizeof(flag));
sum += find(map[i]);
}
}
printf("%d\n", sum/2);
for(i = 0; i < total; i++)
{
if(map[i] != -1 && pre[i] != -1 && !vis[pre[i]] && !vis[i])
{
vis[pre[i]] = vis[i] = true;
printf("(%d,%d)--(%d,%d)\n", i/m+1, i%m+1, pre[i]/m+1, pre[i]%m+1);
}
}
printf("\n");
}
return 0;
}
- 大小: 6.2 KB
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
HDU最全ac代码
hdu动态规划算法集锦
自己做的HDU ACM已经AC的题目
hdu题目分类
HDU图论题目分类
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码
Hdu 1237 解题代码