首页 > ACM题库 > HDU-杭电 > HDU 4185-Oil Skimming-分治-[解题报告]HOJ
2015
05-23

HDU 4185-Oil Skimming-分治-[解题报告]HOJ

Oil Skimming

问题描述 :

Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One such oil baron has a special plane that can skim the surface of the water collecting oil on the water’s surface. However, each scoop covers a 10m by 20m rectangle (going either east/west or north/south). It also requires that the rectangle be completely covered in oil, otherwise the product is contaminated by pure ocean water and thus unprofitable! Given a map of an oil slick, the oil baron would like you to compute the maximum number of scoops that may be extracted. The map is an NxN grid where each cell represents a 10m square of water, and each cell is marked as either being covered in oil or pure water.

输入:

The input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of ‘#’ represents an oily cell, and a character of ‘.’ represents a pure water cell.

输出:

The input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of ‘#’ represents an oily cell, and a character of ‘.’ represents a pure water cell.

样例输入:

1
6
......
.##...
.##...
....#.
....##
......

样例输出:

Case 1: 3

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4185

题意,在一个N*N的矩阵里寻找最多有多少个##”(横着竖着都行)。

KM二分匹配就不说了。

刚开始不会建图,最多有600*600个“#”,那岂不是要开600*600*600*600的数组Orz…..


不过事实证明我想多了,数据没那么狠!

#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
int map[700][700],p[700][700],vis[700],x[700],y[700],t,n,num,k;
char s[700][700];
bool search(int i)
{
    int j;
    for(j=0;j<num;j++)
    {
        if(map[i][j]&&!vis[j])
        {
            vis[j]=1;
            if(!y[j]||search(y[j]))
            {
                y[j]=i;
                x[i]=j;
                return true;
            }
        }
    }
    return false;
}
int match()
{
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
    int ans=0;
    for(int i=0;i<num;i++)
    {
        memset(vis,0,sizeof(vis));
        if(search(i)) ans++;
    }
    return ans/2;//无向图,要除以2
}
int main()
{
    scanf("%d",&t);
    k=1;
    while(t--)
    {
        int i,j;
        num=0;
        memset(map,0,sizeof(map));
        scanf("%d",&n);
        for(i=0;i<n;i++)
        scanf("%s",s[i]);

        for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        if(s[i][j]=='#')
        p[i][j]=num++;

        for(i=0;i<n;i++)
        for(j=0;j<n;j++)
        {
            if(s[i][j]=='#')
            {
                if(j<n-1&&s[i][j+1]=='#')
                {
                    map[p[i][j]][p[i][j+1]]=1;
                    map[p[i][j+1]][p[i][j]]=1;
                }
                if(i<n-1&&s[i+1][j]=='#')
                {
                    map[p[i][j]][p[i+1][j]]=1;
                    map[p[i+1][j]][p[i][j]]=1;
                }
            }
            //map[i][j]='.';
        }
        int p=match();
        printf("Case %d: %d\n",k++,p);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/zhuhuangjian/article/details/8231800