首页 > 搜索 > DFS搜索 > HDU 1507 Uncle Tom’s Inherited Land*-分治-[解题报告] C++
2013
12-12

HDU 1507 Uncle Tom’s Inherited Land*-分治-[解题报告] C++

Uncle Tom’s Inherited Land*

问题描述 :

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

输出:

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.

样例输入:

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

样例输出:

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)

点击打开链接

题目有指出 ( (N x M) – K <= 50), 所以最多也就 50 个点. 然后遍历图上



每一个可行点点, 把它和他 上下左右的可行点连边, 最后就得到了一个二分图, 然后直接



最大匹配, 不过结果有问题, 分析不透彻, 出现了重边. 再 分析下, 因为相邻块之间的下标



和存在奇偶关系, 所以只取 偶数 或 奇数点 建图就行了.

#include"stdio.h"
#include"string.h"
#define N 110
struct node
{
    int x,y;
}aa[51];
int hash[N][N],a[N][N];
int map[N][N],v[N],mark[N];
int n,m,cnt;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
void get_map()
{
    int i,j,k,x,y;
    scanf("%d",&k);
    memset(hash,0,sizeof(hash));
    while(k--)
    {
        scanf("%d%d",&x,&y);
        hash[x][y]=1;
    }
    cnt=1;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
            if(hash[i][j]==0)
            {
                a[i][j]=cnt;
                aa[cnt].x=i;
                aa[cnt].y=j;
                cnt++;
            }
    }
    memset(map,0,sizeof(map));
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
            if(hash[i][j]==0)
            {
                for(k=0;k<4;k++)
                {
                    x=i+dx[k];
                    y=j+dy[k];
                    if(x>=1&&x<=n&&y>=1&&y<=m&&hash[x][y]==0)
                    {
                        map[a[i][j]][a[x][y]]=1;
                        map[a[x][y]][a[i][j]]=1;
                    }
                }
            }
    }
}
int dfs(int k)
{
    int i;
    for(i=1;i<cnt;i++)
    {
        if((aa[i].x+aa[i].y)%2==1)
            continue;
        if(map[k][i]&&!v[i])
        {
            v[i]=1;
            if(mark[i]==-1||dfs(mark[i]))
            {
                mark[i]=k;
                return 1;
            }
        }
    }
    return 0;
}
void output()
{
    int i;
    for(i=1;i<cnt;i++)
        if(mark[i]!=-1)
        printf("(%d,%d)--(%d,%d)\n",aa[i].x,aa[i].y,aa[mark[i]].x,aa[mark[i]].y);
}
void solve()
{
    int i,count;
    count=0;
    memset(mark,-1,sizeof(mark));
    for(i=1;i<cnt;i++)
    {
        if((aa[i].x+aa[i].y)%2==0)
            continue;
        memset(v,0,sizeof(v));
        if(dfs(i))
            count++;
    }
    printf("%d\n",count);
    output();
    printf("\n");
}
int main()
{
    while(scanf("%d%d",&n,&m)!=-1)
    {
        if(!n&&!m)
            break;
        get_map();
        solve();
    }
    return 0;
}

解题报告转自:http://blog.csdn.net/yangyafeiac/article/details/7826132


  1. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.

  2. “再把所有不和该节点相邻的节点着相同的颜色”,程序中没有进行不和该节点相邻的其他节点是否相邻进行判断。再说求出来的也不一样是颜色数最少的