HDU 4185 Oil Skimming (最大匹配)



Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2522    Accepted Submission(s): 1041


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

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

Output
For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted.
 

Sample Input
 
    
1 6 ...... .##... .##... ....#. ....## ......
 

Sample Output
 
    
Case 1: 3
 

Source
The 2011 South Pacific Programming Contest


题意:

一块区域n*n,'#'表示油,'.'表示水,每次能挖1*2的矩形(横竖任意),且不能挖到水,而且不能交叉挖,问最多可以挖几下。


思路:

经典二分图模型,这里的突破点是, 不能交叉挖到。。一个矩形只挖两块, 也就是说,某一块挖了,只能在他四个方向选一个挖, 这一块不能再被挖, 通过这构造二分图了~枚举每一个#,跟他周围四个是#的连边, 这样就构造了一个无向图,然后匈牙利最大匹配,因为是无向的,所以/2;


以后在矩形里的问题,而且有明显互斥性,都可以想想二分图~,或者说凡是有互斥性的题目,都可以想一下~


#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 605;
char maze[maxn][maxn];
vector v[maxn];
int book[maxn], match[maxn], id[maxn][maxn], dir[4][2] = {0,1,0,-1,1,0,-1,0};
int Find(int x)
{
    for(int i = 0; i < v[x].size(); i++)
    {
        int to = v[x][i];
        if(book[to]) continue;
        book[to] = 1;
        if(!match[to] || Find(match[to]))
        {
            match[to] = x;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int t, n, ca = 1;
    cin >> t;
    while(t--)
    {
        memset(match, 0, sizeof(match));
        for(int i = 0; i < maxn; i++)
            v[i].clear();
        int cnt = 1;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
            {
                scanf(" %c", &maze[i][j]);
                if(maze[i][j] == '#') id[i][j] = cnt++;
            }
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
            {
                if(maze[i][j] == '.') continue;
                for(int k = 0; k < 4; k++)
                {
                    int tx = i + dir[k][0];
                    int ty = j + dir[k][1];
                    if(tx < n && tx >= 0 && ty < n && ty >= 0 && maze[tx][ty] == '#')
                        v[id[i][j]].push_back(id[tx][ty]);
                }
            }
        int ans = 0;
        for(int i = 1; i <= cnt; i++)
        {
            memset(book, 0, sizeof(book));
            ans += Find(i);
        }
//        cout << ans << endl;
        printf("Case %d: %d\n", ca++, ans/2);
    }
    return 0;
}


Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2522    Accepted Submission(s): 1041


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

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

Output
For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted.
 

Sample Input
 
    
1 6 ...... .##... .##... ....#. ....## ......
 

Sample Output
 
    
Case 1: 3
 

Source
The 2011 South Pacific Programming Contest

你可能感兴趣的:(二分图/匹配,ACM)