hdu1507——Uncle Tom's Inherited Land*

Uncle Tom‘s Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2114    Accepted Submission(s): 867

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

South America 2002 - Practice

Recommend

LL   |   We have carefully selected several similar problems for you:  1281 1528 1498 1533 1532

二分匹配。建边的根据是枚举全部可行的点,然后枚举周围四个方向。输出方案根据mark数组

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
const int maxn = 10010;
struct node
{
    int to;
    int next;
}edge[4 * maxn];  

int vis[maxn];
int head[maxn];
int mark[maxn];
bool used[maxn];
bool mat[110][110];
int cnt[110][110];
int tot;
int n, m;
int index;  

void addedge(int from, int to)
{
    edge[tot].to = to;
    edge[tot].next = head[from];
    head[from] = tot++;
}  

bool dfs(int x)
{
    for (int i = head[x]; i != -1; i = edge[i].next)
    {
        if (!used[edge[i].to])
        {
            used[edge[i].to] = 1;
            if (mark[edge[i].to] == -1 || dfs(mark[edge[i].to]))
            {
                mark[edge[i].to] = x;
                return true;
            }
        }
    }
    return false;
}  

int hungary()
{
    memset(mark, -1, sizeof(mark));
    int ans = 0;
    for (int i = 0; i < index; i++)
    {
        memset(used, 0, sizeof(used));
        if (dfs(i))
            ans++;
    }
    return ans;
}  

int main()
{
    int k;
    while (~scanf("%d%d", &n, &m))
    {
    	if (!n && !m)
    	{
    		break;
    	}
    	scanf("%d", &k);
    	map<int, int>qu;
    	qu.clear();
        memset(head, -1, sizeof(head));
        memset(mat, false, sizeof(mat));
        memset(vis, -1, sizeof(vis));
        tot=0;
        int x, y;
        for (int i = 0; i < k; i++)
        {
            scanf("%d%d", &x, &y);
            x--;
            y--;
            mat[x][y] = true;
        }
        index = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (!mat[i][j])
                {
                	cnt[i][j] = index++;
                	qu[index - 1] = i * m + j;
                }

        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
            {
                if (!mat[i][j])
                {
                    if (i > 0 && !mat[i - 1][j])
                        addedge(cnt[i][j], cnt[i - 1][j]);
                    if (i < n-1 && !mat[i + 1][j])
                        addedge(cnt[i][j], cnt[i + 1][j]);
                    if (j > 0 && !mat[i][j - 1])
                        addedge(cnt[i][j], cnt[i][j - 1]);
                    if (j < m - 1 && !mat[i][j + 1])
                        addedge(cnt[i][j], cnt[i][j + 1]);
                }
            }
        int res = hungary();
        printf("%d\n", res / 2);
        for (int i = 0; i < index; ++i)
        {
        	if (mark[i] != -1)
        	{
        		int a = qu[i];
        		int b = qu[mark[i]];
        		int x1 = a / m + 1;
        		int y1 = a % m + 1;
        		int x2 = b / m + 1;
        		int y2 = b % m + 1;
        		if (vis[a] == -1 && vis[b] == -1)
        		{
        			printf("(%d,%d)--(%d,%d)\n", x1, y1, x2, y2);
        			vis[a] = b;
        			vis[b] = a;
        		}
        	}
        }
        printf("\n");
    }
    return 0;
} 

hdu1507——Uncle Tom's Inherited Land*

时间: 2024-08-02 11:04:49

hdu1507——Uncle Tom&#39;s Inherited Land*的相关文章

HDOJ 1507 Uncle Tom&amp;#39;s Inherited Land*

直接对每一个格子进行dfs结果除以2能够得到答案可是有大量反复的结果,不好输出答案. 能够仅仅对横纵坐标相加是奇数的格子dfs.... Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1728    Accepted Submission(s): 723 Special Judge

HDU1507 Uncle Tom&#39;s Inherited Land*

题目是跟 zoj1516是一样的,但多了匹配后的输出 详解zoj1516可见http://www.cnblogs.com/CSU3901130321/p/4228057.html 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 using namespace std; 5 6 const int maxn = 55; 7 int n , m , k; 8 int g[maxn][maxn] ,

hdu1507——Uncle Tom&#39;s Inherited Land*

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2114    Accepted Submission(s): 867 Special Judge Problem Description Your old uncle Tom inherited a piece of land fr

HDU1507 Uncle Tom&#39;s Inherited Land* 二分图匹配 匈牙利算法 黑白染色

原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1*2的矩形覆盖所有的不废的点,并且不重叠,问最多可以覆盖多少个1*2的矩形,输出方案,有SPJ. 输入描述: 多组数据,每组首先两个数n,m(如果n和m为0,则结束程序) 然后给出k 然后给出k个二元组(x,y)表示废点的坐标. 题解 按照前两片博文的算法已经不行了,因为方案不对了. 所以我们要进行

HDU——T 1507 Uncle Tom&#39;s Inherited Land*

http://acm.hdu.edu.cn/showproblem.php?pid=1507 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3671    Accepted Submission(s): 1554Special Judge Problem Description Your old uncle Tom inherited

ZOJ1516 Uncle Tom&#39;s Inherited Land(二分图最大匹配)

一个经典的构图:对格子进行黑白染色,黑白的点分别作XY部的点. 这一题的边就是可以出售的单位面积2的土地,边的端点就是这个土地占用的X部和Y部的两个点. 这样就建好二分图,要求最多土地的答案显然是这个二分图的最大边独立集,也就是最大匹配. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 using namespace std; 6 #define

ZOJ 1516 Uncle Tom&#39;s Inherited Land

题目大意: 除去那些作为荷塘的土地块,将剩余的土地希望每次将两块相邻的地一起卖出,最多能卖出多少种这样的由相邻土地 合成的长方形土地块 很明显的二分图问题,但是要考虑如何建模 一个长方形土地总是由相邻的两块地组成,那么我们就将相邻的两块地一块放在X集合,一块放在Y集合 所有放在X集合中的土地互不影响(也就是任意两个在X中的土地不形成长方形) 那么我们可以看作土地上 0101010 1010101 0101010 1010101 比如这样标注的,那么0所对应的空地就放入集合X,并不断添加一个X的标

XTU 二分图和网络流 练习题 B. Uncle Tom&#39;s Inherited Land*

B. Uncle Tom's Inherited Land* Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class name: Main Special Judge Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had bee

hdu 1507 Uncle Tom&#39;s Inherited Land*(二分)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1853    Accepted Submission(s): 769 Special Judge Problem Description Your old uncle Tom inherited a piece of land fr