hdu 4185 Oil Skimming

来源:互联网 发布:网络电视要不要机顶盒 编辑:程序博客网 时间:2024/05/23 11:54

http://acm.hdu.edu.cn/showproblem.php?pid=4185

Oil Skimming

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
16.......##....##.......#.....##......
Sample Output
Case 1: 3 

题意:在大海里有一些石油 ‘#’表示石油, ‘.’表示水,有个人有一个工具可以回收这些石油,不过只能回收1*2大小的石油块,里面不能含有海水,要不就没办法使用了,求出来最多能回收多少块石油

与poj 3020 Antenna Placement 比较相似,稍微修改一下代码就正确了吐舌头

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define N 1350#define INF 0xfffffff#define PI acos (-1.0)#define EPS 1e-8const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int n, cnt, used[N], g[N][N], M[N][N], vis[N];char s[N][N];int DFS (int u);int solve ();///求最大匹配int main (){    int t, flag = 1;;    scanf ("%d", &t);    while (t--)    {        scanf ("%d", &n);        cnt = 0;        memset (M, 0, sizeof (M));        memset (g, 0, sizeof (g));        memset (used, 0, sizeof (used));        for (int i=0; i<n; i++)        {            getchar ();            for (int j=0; j<n; j++)            {                scanf ("%c", &s[i][j]);                if (s[i][j] == '#')                    M[i][j] = cnt++;            }        }        for (int i=0; i<n; i++)        {            for (int j=0; j<n; j++)            {                if (s[i][j] == '#')                for (int k=0; k<4; k++)                {                    int xx = i+dir[k][0], yy = j+dir[k][1];///四个方向搜索                    if (xx>=0 && xx<n && yy>=0 && yy<n && s[xx][yy] == '#')                        g[ M[i][j] ][ M[xx][yy] ] = 1, g[ M[xx][yy] ][ M[i][j] ] = 1;///重新构图                }            }        }        int ans = solve ();        printf ("Case %d: %d\n", flag++, ans/2);    }    return 0;}int solve (){    int ans = 0;    for (int i=0; i<cnt; i++)    {        memset (vis, false, sizeof (vis));        if (DFS (i)) ans++;    }    return ans;}int DFS (int u){    for (int i=0; i<cnt; i++)    {        if (!vis[i] && g[u][i])        {            vis[i] = true;            if (!used[i] || DFS (used[i]))            {                used[i] = u;                return true;            }        }    }    return false;}



0 0
原创粉丝点击