HDU-1507-Uncle Tom's Inherited Land*

本文探讨了一种解决特殊土地划分问题的算法,通过构建二分图并运用深度优先搜索来确定可售土地的最大数量。文章详细介绍了算法的实现过程,包括初始化、深度优先搜索求解等步骤,并提供了一个具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://vjudge.net/problem/HDU-1507

题意:

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). 

思路:

根据矩阵x,y的和建立二分图,同时不处理池塘。

输出路径根路据Link数组,找到每个边对应左端点的编号,同时结构体排序,使编号小的在前,再遍历数组查找即可。

代码:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
using namespace std;

typedef long long LL;
const int MAXN = 1e4+10;
int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

struct Node
{
    int x, y;
    int pos;
    Node(int x, int y, int pos):x(x), y(y), pos(pos){}
    bool operator < (const Node & that)const
    {
        return this->pos < that.pos;
    }
};

vector<int> G[MAXN];
int Dis[200][200];
int Link[MAXN], Vis[MAXN];
int n, m, k;
int mid;
int cnt1, cnt2;

void Init()
{
    for (int i = 1;i <= n;i++)
        G[i].clear();
}

bool Dfs(int x)
{
    for (int i = 0;i < G[x].size();i++)
    {
        int node = G[x][i];
        if (Vis[node] == 0)
        {
            Vis[node] = 1;
            if (Link[node] == -1 || Dfs(Link[node]))
            {
                Link[node] = x;
                return true;
            }
        }
    }
    return false;
}

int Solve()
{
    memset(Link, -1, sizeof(Link));
    int cnt = 0;
    for (int i = 1;i <= cnt1;i++)
    {
        memset(Vis, 0, sizeof(Vis));
        if (Dfs(i))
            cnt++;
    }
    return cnt;
}

int main()
{
    while (~scanf("%d%d", &n, &m) && n)
    {
        cnt1 = cnt2 = 0;
        memset(Dis, 0, sizeof(Dis));
        cin >> k;
        int x, y;
        for (int i = 1;i <= k;i++)
        {
            cin >> x >> y;
            Dis[x][y] = -1;
        }
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= m;j++)
            {
                if (Dis[i][j] != -1)
                {
                    if ((i+j)%2 == 1)
                        Dis[i][j] = ++cnt1;
                    else
                        Dis[i][j] = ++cnt2;
                }
            }
        }
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= m;j++)
            {
                if ((i+j)%2 == 1 && Dis[i][j] != -1)
                {
                    for (int k = 0;k < 4;k++)
                    {
                        int tx = i+Next[k][0];
                        int ty = j+Next[k][1];
                        if (tx < 1 || tx > n || ty < 1 || ty > m)
                            continue;
                        if (Dis[tx][ty] == -1)
                            continue;
                        G[Dis[i][j]].push_back(Dis[tx][ty]);
                    }
                }
            }
        }
        int res = Solve();
        set<Node> kv;
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= m;j++)
            {
                if ((i+j)%2 == 0 && Dis[i][j] != -1 && Link[Dis[i][j]] != -1)
                {
                    kv.emplace(i, j, Link[Dis[i][j]]);
                }
            }
        }
        cout << res << endl;
        auto it = kv.begin();
        for (int i = 1;i <= n;i++)
        {
            for (int j = 1;j <= m;j++)
            {
                if ((i+j)%2 == 1 && Dis[i][j] == it->pos)
                {
                    printf("(%d,%d)--(%d,%d)\n", i, j, it->x, it->y);
                    ++it;
                }
            }
        }
        for (int i = 1;i <= cnt1;i++)
            G[i].clear();
    }

    return 0;
}

  

转载于:https://www.cnblogs.com/YDDDD/p/10871591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值