您的位置:首页 > 其它

HDU 4185 Oil Skimming 【离散化二分匹配 黑白染色】

2016-10-05 16:16 441 查看

Oil Skimming

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2437    Accepted Submission(s): 994


[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

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

 

[align=left]Sample Output[/align]

Case 1: 3

 

[align=left]Source[/align]
The 2011 South Pacific Programming Contest

rt格子有600*600个,每个矩形可以覆盖1*2的格子(横竖均可,但是不允许出现在空白格子上)问最多可以存在多少个矩形

做法:只为#格子编号,犯的sb错误是k写成了i

#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define MAXN 610
#define inf 0x3f3f3f3f
int n;//u,v数目
int linker[MAXN*MAXN];
bool used[MAXN*MAXN];
vector<int>exi[MAXN*MAXN];
bool dfs(int u)//从左边开始找增广路径
{
for(int i=0;i<exi[u].size();i++)
{
int v=exi[u][i];
if(!used[v])
{
used[v]=true;
if(linker[v]==-1||dfs(linker[v]))
{
linker[v]=u;
return true;
}
}
}
return false;
}
int len;
int hungary()
{
int res=0;
memset(linker,-1,sizeof(linker));
for(int i=1;i<=len;i++)
{
memset(used,0,sizeof(used));
if(dfs(i))res++;
}
return res;
}
char str[MAXN][MAXN];
int id[MAXN][MAXN];
int to[2][2]={1,0,0,1};
int main()
{
//freopen("cin.txt","r",stdin);
// for(int i=0;i<2;i++)for(int j=0;j<2;j++)printf("i=%d,j=%d,to=%d\n",i,j,to[i][j]);
int t,cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<=n*n;i++)exi[i].clear();
memset(id,0,sizeof(id));
len=0;
for(int i=1;i<=n;i++)
{
scanf("%s",str[i]+1);
for(int j=1;j<=n;j++)
{
if(str[i][j]=='#')
id[i][j]=++len;
}
}
// for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++)printf("%d ",id[i][j]); puts("");}
//for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++)printf("%c ",str[i][j]); puts("");}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(str[i][j]=='#')
{
for(int k=0;k<2;k++)
{
int tx=i+to[k][0];
int ty=j+to[k][1];
// printf("i=%d,j=%d,tx=%d,ty=%d\n",i,j,tx,ty);
if(tx<1||tx>n||ty<1||ty>n||str[tx][ty]!='#')continue;
// printf("id[i][j]=%d,id[tx][ty]=%d\n",id[i][j],id[tx][ty]);
exi[id[i][j]].push_back(id[tx][ty]);
exi[id[tx][ty]].push_back(id[i][j]);
}
}
}
}

printf("Case %d: %d\n",cas++,hungary()/2);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: