您的位置:首页 > 其它

hdu 4185 Oil Skimming (最大匹配)

2017-02-23 17:01 369 查看
题意:

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

思路:

给每块油标号,相连的油建立关系,然后跑一下匈牙利,/2即为答案。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 1e3+5;
int n, idNum, id[maxn][maxn], match[maxn], vis[maxn];
vector<int> g[maxn];

bool dfs(int x)
{
for(int i = 0; i < g[x].size(); i++)
{
int to = g[x][i];
if(!vis[to])
{
vis[to] = 1;
if(!match[to] || dfs(match[to]))
{
match[to] = x;
return 1;
}
}
}
return 0;
}

int Hungary()
{
int res = 0;
memset(match, 0, sizeof(match));
for(int i = 1; i < idNum; i++)
{
memset(vis, 0, sizeof(vis));
res += dfs(i);
}
return res;
}

int main(void)
{
int t, ca = 1;
cin >> t;
while(t--)
{
for(int i = 0; i < maxn; i++)
g[i].clear();
memset(id, 0, sizeof(id));
idNum = 1;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
{
char ch;
scanf(" %c", &ch);
if(ch == '#') id[i][j] = idNum++;
}
int next[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
if(id[i][j])
{
for(int k = 0; k < 4; k++)
{
int nx = i+next[k][0];
int ny = j+next[k][1];
if(id[nx][ny])
g[id[i][j]].push_back(id[nx][ny]);
}
}
printf("Case %d: %d\n", ca++, Hungary()/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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  匈牙利