ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU-1507-Uncle Tom's Inherited Land*

2019-05-15 19:49:15  阅读:265  来源: 互联网

标签:HDU Land int great pos property uncle Tom include


链接: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;
}

  

标签:HDU,Land,int,great,pos,property,uncle,Tom,include
来源: https://www.cnblogs.com/YDDDD/p/10871591.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有